Why don’t C# and Java support multiple inheritance?

C++ actually allows a class to inherit from more than one class, and this is referred to as multiple inheritance. But in C# and Java, classes are only allowed to inherit from a single parent class, which is called single inheritance.

Multiple inheritance allows people to create classes that combine aspects of different classes and their corresponding hierarchies. This is something that is quite common because applications often need to use the frameworks of different classes to achieve the desired functionality. Suppose that you are trying to create a class that models the animal known as a Liger, which is the result of having a Tiger and a Lion mate. Then you would create a class called Liger that derives from both the Tiger and Lion classes – so the Liger class would have to use multiple inheritance since it is deriving or inheriting from 2 different classes.

What is wrong with multiple inheritance?

The main problem with multiple inheritance is that there can be times when the results of using multiple inheritance will be uncertain. The best example of this is the classic problem known as the diamond problem where a class inherits from 2 different classes, but those 2 different classes inherit from the same class, like in the graphic below (where class D derives from both classes B and C, and classes B and C both derive from class A:

And here is what the code for that example would look like:

class A {
           protected: 
               bool testing;
};

class B: public A { };

class C: public A { };

class D: public B, public C  {
  public:
    void setTesting ( bool xTesting)  {
            testing = xTesting; // this is uncertain
           }
};

In the code above we have the testing data member which is defined by class A. But, the problem is that class D derives from both classes B and C, which both derive from class A. This means that there are essentially 2 copies of the testing flag that are available because there are 2 instances of A in D’s class hierarchy. So, this creates a problem because which copy of the testing flag will be set? And the compiler will give an error and say that the reference to testing in class D is ambiguous.

But, there are some fixes to this problem. One fix is to make it very clear which classe’s version of testing is going to be set:

B :: testing = xTesting;  // use B's version of testing

The other fix for this problem is to declare B and C as virtual base classes. This allows only one copy of A to be created for class D, and that clears up any ambiguities. Read more here to understand that in more depth.

Alternatives to multiple inheritance

As you can probably see by now, having multiple inheritance built into a language can create problems. So, in the interest of keeping things simple, the creators of Java and C# decided not to allow multiple inheritance. However, there is an alternative to multiple inheritance, known as interfaces – and you can read more about interfaces here

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.