Introduction
In the first part of this article, I showed how to effectively inherit public methods from additional parent classes. However, in the conclusion to that article, I said there were a number of gaps in the implementation which I'd like to address in this second part.
Gaps in the implementation
What are the areas still to be covered?
1. Child isn't really a Mother (no pun intended!). For example, we can't do any of these which would work if the Child 'really' inherited from the Mother.
A. Mother m = new Child();
B. Mother m = (Mother)new Child();
C. Mother m = new Child() as Mother;
D. If (c is Mother) Console.WriteLine("Child is a Mother")
2. We can't inherit protected methods.
3. If the Mother inherited some interfaces, then the Child would not automatically do so.
4. Having to change the name of the inherited version of Mother's MethodA to MethodD is not very satisfactory.
5. There is no way to tell by looking at the header of the Child class that it 'multiply inherits' Mother.
Of these, 1(a) and 1(b) are easy to deal with. All we need to do is provide overrides of the 'implicit' and 'explicit' operators in the Child class. However, this won't help with 1(c) and 1(d) which ignore custom conversions.
Suppose instead we implement an interface (IMother say) which has no members but is just a 'marker' interface. This would deal with 1(c) 1(d) and also 5.
We can also deal with 2. quite easily because protected members are visible to our private nested class NMother which can then expose them (via a public member) to the Child class itself. Incidentally, this public member would not be accessible to classes other than Child because its accessibility is limited by the nested class itself being private.
3. This can be addressed by including the interfaces in the Child's implementation list - the members themselves will already be implemented.
Finally, 5. could be dealt with by adding those members whose names are duplicated to the IMother interface and then implementing them 'explicitly'. They could then be accessed using their original names via an IMother reference.
So, this gives us the following code.
When you build and run this code, the results should be.
Conclusion
So we now have a reasonably complete implementation of multiple inheritance.
If we wanted another class (Child2 say) to 'multiply inherit' from Mother, then all we need to do is to copy the partial class with the Mother stuff in it to a partial class of Child2. Using partial classes in this way enables us to keep everything together and facilitate code reuse.