Thursday, November 22, 2012

Why Java does not support Multiple Inheritance

Consider the one of the goal behind the java Language :
  • Simple,object oriented and familiar 

The reason behind omitting the Multiple inheritance from the java language is mostly from the "Simple ,objected oriented & familiar" goal.
Java people want the java language to be simple so that most of developer learn easily.They make the language similar to c++ as possible without carrying the complexity from c++ language.

Java does not support multiple inheritance

This is the main point of discussion whether java support multiple inheritance or not. There is big misconception that, java support multiple inheritance through interface. Answer is NO. Java does not support multiple inheritance at all. If you don't believe this read the first goal of java :).

Basically interface give the flexibility over concrete class and we have the option of implementing multiple interface.
Implement class is the one that is going to add properties and behavior. It is not getting the implementation from the parent class. But, in multiple inheritance we are inheriting the properties & behavior from multiple classes in a single class.

Now we are sure that there is no support for multiple inheritance in java. 
Reasons are as follow :


In order to enforce simplicity is one the main reason for omitting the multiple inheritance.
We can consider the ambiguity of Diamond problem
Consider a class A with latte() method and then class B and class C derived from class A and has own latte() method implementation.
Consider class D derive from Class B & Class C using multiple inheritance.

If we refer to latte() method, compiler will not able to decide which latte() method.

This is called the Diamond problem.

                             Class A----latte()

                          /                           \
                                   /                              \
Class B -- latte()                                 Class C --latte()
                        \                             /
                         \                           /
                             Class D --latte()

In java this can never be occur because there is not multiple inheritance in java.

Here if even two interfaces going to be have the same method, the implementing class is going to have the one method and that will take care by the implementer class.

A language that allows the the Deadly diamond of Death can lead to some ugly complexities, because you have to have special rules deal with the potential ambiguities. And extra rules means extra work for you both in learning those rules and watching out for those "special rules. Java is supposed to be simple with consistent rules that don't blow up under some scenarios. So java protects you from having  to think about the Deadly Diamond of Death.

Complex design & rarely used
Multiple inheritance make the design complicate and create problem during casting & constructor chaining. 
There are not many scenario where you need multiple inheritance its wise decision to omit for the sake of simplicity.
Also java omit this ambiguity with the help of supporting single inheritance with interface. Since interface has only method declaration and does not provide any implementation there will be only one implementation of  specific method.

Now with Java 8, the interface body can contain abstract methodsdefault methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation). 

QAgain one question come to mind that if two interface are providing default method with similar signatures and class is extending both the interface, will it not be again diamond problem?
Yes, you are thinking right but Java doesn't allow you to implement interface with has same default method.

If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...


  1. Hi Pardeep,
    Thanks for writing java-latte.. Its very informative keep up the good work.
    Your blogs always help me.
    Just a request can you update this blog with how default method in java 8 will affect this.

    1. It's already present in my blog post :