Multiple Inheritance and the diamond problem

Starting this thread I assume everybody knows what inheritance is in Object Oriented programming and what benefits it can provide. I will not delve into the basics of objet inheritance. This post rather is focused more on multiple inheritance and the problems it faces.

So, one big problem of multiple inheritance is the Diamond Problem. What is that you may ask? Well imagine inheritance distributed like the following.

The above image shows D who inherits from both B and C and both of those inherit from A
Now imagine A is an abstract class (does not need to be for the diamond problem to exist but it makes it more visible) and contains a public abstract method called Jump(). Both B and C need to implement that method in their own specific ways. Then D inherits from both of these classes. What will happen when somebody calls jump on D?
There will ambiguity on which of the implementations of Jump() to call.

And for this reason both Java and C# do not allo multiple inheritance. They do allow for multiple interface implementation though and one future feature of C# connects interfaces with multiple inheritance problem. We will discuss that one next week.

Leave a Reply