Java Multiple Inheritance
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.
Java doesn't support Multiple Inheritance
Example 1: Multiple Inheritance with Classes gives Error in Java
import java.io.*;
// First Parent Class
class Parent1{
void fun() { System.out.println("Parent1");
}
}
// Second Parent Class
class Parent2{
void fun() { System.out.println("Parent2");
}
}
// Inheriting Properties of
// Parent1 and Parent2
class Test extends Parent1, Parent2
{
// main method
public static void main(String args[])
{
// Creating instance of Test
Test t = new Test();
t.fun();
}
}
Output: Compilation error is thrown

Achieving Multiple Inheritance with Interfaces
Java allows a class to implement multiple interfaces, thus supporting multiple inheritance safely.
interface A{
default void display(){
System.out.println("Interface A display");
}
}
interface B{
default void display(){
System.out.println("Interface B display");
}
}
class C implements A, B{
@Override
public void display(){
// Resolving conflict
A.super.display();
B.super.display();
System.out.println("Class C display");
}
}
public class Main {
public static void main(String[] args){
C obj = new C();
obj.display();
}
}
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().
// Interface 1
interface PI1 {
default void show(){
System.out.println("Default PI1");
}
}
// Interface 2
interface PI2 {
default void show(){
System.out.println("Default PI2");
}
}
class TestClass implements PI1, PI2 {
// Overriding default show method
@Override
public void show(){
PI1.super.show();
PI2.super.show();
}
// Declared new Method
public void showOfPI1() {
PI1.super.show();
}
// Declared new Method
public void showOfPI2() {
PI2.super.show();
}
// main Method
public static void main(String args[]) {
// Instance of Class
TestClass d = new TestClass();
// Using show Method
d.show();
// Executing the Methods
System.out.println("Now Executing showOfPI1()" +
" showOfPI2()");
d.showOfPI1();
d.showOfPI2();
}
}
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.