Why multiple Inheritance is not allowed in Java?

Q. Why multiple Inheritance is not allowed in Java?

A. We always want to extend multiple classes so that we inherits the features of the multiple classes.
For example:
class C extends A,B
{ }
but the above declaration is not valid in Java. In Java ,one class cannot extends from multiple classes. we can extend only from single class. But Java supports multilevel inheritance, means it will have multiple ancestors .For example

class C extends B{}
class B extends A{}

Here,  C class inherits the features from the multiple ancestors (A and B).

In C++ ,One can extend multiple classes simultaneously that is the declaration we have done for class A above is correct in case of C++.Capability of extending multiple classes is called as "Multiple Inheritance".Java creators thought a lot about to allow the multiple inheritance,but they were messed up after using the multiple inheritance. Like in this case ,if a class extended two classes and if both classes has methods in common then how the methods will be inherited and how we will come to know that which method is called of which class. That is why they excluded the multiple inheritance in Java. There is a famous problem which is faced during the multiple inheritance application called as "Deadly Diamond of Death".This is named so because the shape of class diagram which is formed after multiple inheritance implementation. The diamond is formed when classes B and C extend A ,and both B and C inherit methods from A. If class D extends both B and C,and both B and C have overridden the methods in class A,class D in theory has inherited the two different implementations of same method.


There is an indirect way by which you can implement the multiple inheritance in Java and this is through extending a class and implementing an interface just like following:-

class C extends B implements A{     //Here A is an interface
// implements methods
}

In this way you can get methods from both class and interface and you can override them according to your use. Remember all methods of interface needs to be overridden in implementing class.

People who read this post also read :



6 comments:

Diamond pattern is one of the reason. here are some more reasons on Why multiple inheritance is not supported in Java .

Thanks a lot for this so detailed and useful piece of knowledge, this question arise many times in mind. And helpful for learners .

sap erp system

Hey Thanks Tani..............
Tani i think u r also running a blog

Thanks so much - good to know multiple inheritance is not a part of Java, was confusing in C++.

Also good link:

Multiple inheritance in Java

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here.
Kindly keep blogging. If anyone wants to become a Java developer learn from Java EE Online Training from India.
or learn thru Java EE Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More