Please take a look at my class structure. I think I would like to have more fun with inheritance than it is possible.
First there is a base abstract class:
public abstract class PolicyDetailed
{
internal abstract DataContainer GetActiveAsset();
}
Next there is another abstract class, which is generic:
public abstract class PolicyDetailed<T> : PolicyDetailed where T : DataContainer
{
internal new abstract T GetActiveAsset();
}
Lastly there is a specific policy class. AccidentContainer inherits from DataContainer:
public class PolicyAccident : PolicyDetailed<AccidentContainer>
{
internal override AccidentContainer GetActiveAsset()
{
return null;
}
}
During compilation I get the following error:
'PolicyAccident' does not implement inherited abstract member 'PolicyDetailed.GetActiveAsset()'
I am not sure what modifiers I should use here to get it to work. Maybe I should also write what I would like to achieve: I have a collection of policy objects of different type (e.g. PolicyAccident, PolicyTravel etc.) which inherit from PolicyDetailed with different types of DataContainer (AccidentContainer, TravelContainer etc.). I would like to call “GetActiveAsset” method on each of them without knowing their specific type and referencing them through PolicyDetailed. At the same time I would like each class to return their specific Datacontainer subclass. Is that possible?
The problem is that you can’t override the non-generic method in the same class as you declare any other method with the same signature.
There are a few options:
By far the simplest is to give the two methods different names. Then you can give an implementation in
PolicyDetailed<T>which just delegates to the new abstract method:You could introduce another level of inheritance, introducing a new method name just for bridging purposes. This is pretty ugly:
You could make the non-generic
PolicyDetailedclass an interface instead, and use explicit interface implementation to declare a new abstract method and still implement the interface.