Question

I have a base abstract class BasePerson. I have another abstract class BaseStudent and this inherits from BasePerson. Here is the example.

public abstract class BasePerson
{
    public string Name{get;set;}
    public string LastName{get;set;}
    public abstract object this[string propertyName]{get;set;}
}

public abstract class BaseStudent : BasePerson
{
    public string Test{get;set;}
    //this class inherits from BasePerson and it forces to override the indexer.
    //I can do this:
    public override object this[string propertyName]
    {
        get{return null;} 
        set
        {
            //do stuff;
        }
    }
}

public class Student : StudentBase
{
    //other properties
}

Now I am not able to force Student class to override the indexer. What should I do to force Student to override the indexer? I can't remove the indexer from BasePerson class.

Help appreciated!

Was it helpful?

Solution

If you want to force it, don't implement it on BaseStudent. Since BaseStudent is abstract, it does not need to implement all the abstract members from BasePerson.

public abstract class BasePerson
{
    public string Name{get;set;}
    public string LastName{get;set;}
    public abstract object this[string propertyName]{get;set;}
}

public abstract class BaseStudent : BasePerson
{
    public string Test{get;set;}
}

public class Student : BaseStudent
{
    //must implement it here since Student isn't abstract!
}

abstract classes don't need to define all the abstract members of their inherited class, so you can feel free to pass up the responsibility to whatever concrete class would implement it. Student isn't defined to be abstract so it must implement whatever members have not already been implemented by the chain of base classes that it has inherited.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top