How multiple inheritance implemented in java?

Multiple inheritance in Java programming is implemented using interfaces. Java does not support multiple inheritance using classes.

In simple term, a class can inherit only one class and multiple interfaces in a java programs. In java terminology, we can say that a class can extend only one class but can implement multiple interfaces.

For example, below inheritance using multiple classes is wrong as two classes cannot be extended or inherited. Class C is inheriting class A and B.

 

Below program example for multiple inheritance is correct as this example is extending only one class A and implementing multiple interface i.e. IB and IC.

 

NOTE: Whenever multiple inheritance is asked in an interview, most of the candidates says java does not support multiple inheritance and that’s it. This answer is partially correct and is not a satisfactory answer and we need to state explicitly that java supports multiple inheritance using interfaces not classes.

Multiple Inheritance Program Example in Java

Here is the complete java program example of multiple inheritance using interfaces. Also, it will extend one class as extending one class in java is allowed.

In this java program, Bird class will extend one class (Color) and use multiple inheritance properties by implementing 2 interfaces i.e. IFlyable and IEatable

Output:
Bird eats
Bird flying
Color Red

Read More