Multiple Inheritance Using Interfaces

  • Eclipse: Oxygen
  • Java: 1.8

Interfaces, as we discussed earlier, is the outline or the prototype for the classes implementing it. Multiple Inheritance is not allowed in Java due to problems such as the diamond problem which causes crashing of applications.

Interfaces are used to perform multiple inheritances which can be seen in the following example.

As we see, in the project we have multiple interfaces which are implemented by Person class because multiple inheritance does not create any ambiguity in interfaces.

Abstract method eat is implemented in a vegan class.

While creating the object of person class and providing its reference to Vegan class we can call all the method written in Person class as well as eat method which is implemented in Vegan class.

Output:

be alive; remain living.

Eats vegan food

Shares his/her thoughts.

life is a journey, keep moving and meet new people.

Contributed by: Poonam Tomar

 


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu