Sunday, 22 April 2018

Why Java doesnot support multiple Inheritance?

When a class can extend more than one class then this capability is known as "multiple inheritance."

But java doesn't support multiple inheritance.
We are going to understand the reason behind java not supporting multiple inheritance.


Reason
The reason that Java's creators chose not to allow multiple inheritance is that it can become quite messy.
The problem is that if a class extended two other classes, and both super classes had, say, a doTask() method, which version of doTask() would the subclass inherit?
Ambiguity arises at this point.

This issue can lead to a scenario known as the "Deadly Diamond of Death," because of the shape of the class diagram that can be created in a multiple inheritance design.

The diamond is formed when classes B and C both extend A, and both B and C inherit a method from A. If class D extends both B and C, and both B and C have overridden the method in A, class D has, in theory, inherited two different implementations of the same method. Drawn as a class diagram, the shape of the four classes looks like a diamond.

Lets understand the same with the help of a diagram :
           
Why multiple inheritance not supported in java?

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.