VOOZH about

URL: https://www.geeksforgeeks.org/java/java-and-multiple-inheritance/

⇱ Java Multiple Inheritance - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Multiple Inheritance

Last Updated : 16 May, 2026

Multiple Inheritance is an object-oriented concept where a class can inherit from more than one parent class. While powerful, it can cause ambiguity when multiple parents have the same methods.

  • If two of the parent classes have a method with the same signature, the compiler cannot determine which one to execute.
  • This ambiguity is the reason Java does not support multiple inheritance with classes.
👁 multiple-inheritance
Multiple Inheritance

Java doesn't support Multiple Inheritance

Java does not support multiple inheritance with classes to avoid ambiguity problems like the Diamond Problem, where a class may inherit the same method from multiple parent classes.

Example 1: Multiple Inheritance with Classes gives Error in Java

Output: Compilation error is thrown

👁 Image

Achieving Multiple Inheritance with Interfaces

Java allows a class to implement multiple interfaces, to supporting multiple inheritance safely.


Output
Interface A display
Interface B display
Class C display

Explanation:

  • Class C implements both interfaces A and B.
  • Both interfaces define a default method display(), causing potential conflicts.
  • C resolves conflict by explicitly calling A.super.display() and B.super.display().
  • Finally, C adds its own logic, achieving multiple inheritance safely.

Example 2: Handling Default Methods (Java 8+)

In Java 8, interfaces can have default methods with implementations. If a class implements multiple interfaces with the same default method, it must override the method or explicitly choose one using InterfaceName.super.method().


Output
Default PI1
Default PI2
Now Executing showOfPI1() showOfPI2()
Default PI1
Default PI2

Explanation:

  • Both interfaces PI1 and PI2 define default method show().
  • TestClass overrides show() to resolve the conflict using super.
  • Additional methods showOfPI1() and showOfPI2() call each interface method explicitly.
  • Without overriding, the compiler throws an error, default methods in multiple interfaces must be handled explicitly.

Note: Using interfaces with default methods in Java is a way to safely solve the diamond problem, allowing a class to inherit behavior from multiple interfaces without ambiguity.

Comment
Article Tags: