Saturday, 20 April 2013

Interview Question #9 Why multiple inheritance is not supported by Java?

Multiple inheritance has a problem known as The Deadly Diamond of Death.

Refer to the following diagram to understand The Deadly Diamond of Death-

Explanation -  

     Suppose you have class C which extends two classes A and B.Lets say A and B both have a function with same prototype eg. public int getData(). Now if we create an object of class C and call the function i.e ObjC.getData() which function will be called? Decision is ambiguous unless we have set some rules and priorities. To avoid such ambiguity Java does not allow multiple inheritance.

A language that allows Deadly Diamond of Death can lead to some ugly complexities, because you have to have special rules to deal with the potential ambiguities.

Java is designed to be simple. Java (unlike C++) protects you from having to think about Deadly Death of Diamond.

To provide similar functionality Java provides interfaces.
So Java can implement two or more interfaces but can extends only one super class.
t> UA-39527780-1 back to top