Multiple inheritance java

Multiple inheritance in java means one class implementing two or more than two interfaces simultaneously. In simple words multiple inheritance in java means one class accessing the states and behavior of two or more than two interfaces simultaneously. Multiple inheritance is shown below.

Multiple inheritance example in java:

Example of multiple inheritance in java is given below.

Why multiple inheritance is not supported in java

Java does not support multiple inheritance because following two reasons:
A)Constructor chaining ambiguity
B)Compilation ambiguity



A)Constructor chaining ambiguity

Whenever we create an object of a class that class constructor calls to its parent class constructor which is nothing but the constructor chaining. So when a class extends two or more than two classes simultaneously and when you try to create an object of subclass then compiler gets confused because which parent class constructor has to call as there are two or  more than two parent classes that’s why Java does not support multiple inheritance.

B)Compilation ambiguity

When a class extends two or more than two classes simultaneously which have same methods which is shown below (the work method is available in both parent class). So when we try to access inherited method in child class then the compiler does not understand which parent class method implementation has to be executed that why multiple inheritance is not possible using class. In below example, father and Mother are parent classes and Son is child class. Both parent classes have work() method. Now assume Son class is extending both parent classes i.e father and Mother. As Son class is extending father and Mother classes, it has work method and when we try to access that work method compiler does not understand which parent class implementation has to executed that’s why java does not allow multiple inheritance using class.

Why multiple inheritance is supported in interface

A)Multiple inheritance is supported in interface because interface has only 100%  abstract methods and we all know abstract method does not have the implementation. So when class implements more than two interfaces and having same abstract methods then complier only checks wheather the mehod has been overridden  or not rather than checking method implementation. So there is no compilation ambiguity that’s why multiple inheritance is supported in interface. Example of multiple inheritance is given below.

B)The interface does not have a constructor. So there is no constructor chaining ambiguity that’s why multiple inheritance is supported in the interface




You may also like...

Leave a Reply