Returning different class types at different levels of inheritance.

What is the 'correct' way to :

1) Enforce derived classes provide an overriden method Parent

AND

2 ) Each Parent returns a different class

So Kid.Parent returns a Father,

Father.Parent returns a GrandFather

All of the classes derive from a base class - let us say Adam. Adam's Parent method cannot be abstract because overriding Adam's method requires that the return type of the 'get' is the same. If Adam has

abstract public Adam Parent { get ; }

I cannot put the following in Father

public override GrandFather Parent { get { return myDad; } }

Is this because .Net C++ supports overriding on the return value



Answer this question

Returning different class types at different levels of inheritance.

  • sopi20

    Well, I understand why strongly typed classes are a good thing ;)

    But enforcing implementation of a Property or method, e.g. by using an interface or an abstract class implies that you want to handle instances of the class and inherited classes generically. Overriden return types would void that advantage as you must know what you are dealing with to use the property or method. So I just do not see the use in it... but you will know what you want to use and need it for...


  • John Hennesey

    You can't have Parent of different types in your hierarchy, this is meaningless. You can have object Parent; for all, but some will return Father, some Grandfather.

  • annag

    If you override a method, the signature of the method must match, so you cannot change parameters or return values.

    I do not really see the point in the typed parent methods ;) - but if your intention is to have the parent method to return strongly typed values, maybe generics will help you:

    abstract public T GetParent<T>() where T : class;

    called e.g. like GrandFather gf = father.GetParent<GrandFather>();

     


  • Akhraden

    Its not meaningless. In fact, Eiffel supports this type of derivation. A derived class is allowed to change the return type of a method, as long as the new return type is derived from the old one. This works because the type restriction of the return value as defined by the base class is still met, but the derived class makes the restriction even stronger. Unfortunately, C# does not support this, as very few OO languages do.
  • Marsha4830

    Thank you all. I can assure Sergey that it is not meaningless as I have used this in the past. I agree with NimRand.

    The question I was really asking was what is the best way to achieve points 1 & 2. John may not yet understand why strongly-typed derived return classes are a good thing :-).

    There are a number of times when you need things like that eg. specialized security rules depending on the type.

    However, I do like his suggestion of the Generic solution. That will take a little thinking about. Maybe I should just wait for

    [IgnoreReturnType]

    abstract object myThing { get ; }


  • Returning different class types at different levels of inheritance.