Multiple Inheritance in Java

Deriving a subclass from multiple superclasses is known as multiple inheritance in Java. In other words, when a single class inherits properties and behaviors from more than one parent class, it is called multiple inheritance.

Some programming languages, such as C++ and Python, support multiple inheritance of classes. However, Java does not support multiple inheritance of classes directly. In Java, a class can extend only one superclass using the extends keyword, but it can implement multiple interfaces using the implements keyword.

Therefore, Java supports single inheritance through classes and multiple inheritance through interfaces. This approach allows a class to inherit behavior and implement multiple types without extending multiple classes.

Look at the below figure, where a class C extends two superclasses, A and B.

Diagram showing why Java does not support multiple inheritance through classes.

Now, look at some different examples of multiple inheritance in the below figure to understand more clearly.

Illustration demonstrating Java does not support multiple inheritance through classes.

In programming languages that support multiple inheritance, it can sometimes increase complexity and cause ambiguity when a class inherits methods with the same signature from multiple superclasses. A well-known example of this ambiguity is the diamond problem.

Java avoids this issue with class inheritance by allowing a class to extend only one superclass. However, similar conflicts can arise when a class implements multiple interfaces that provide default methods with the same signature.

Java Does Not Support Multiple Inheritance Through Classes


If a subclass inherits from a superclass, it can inherit the superclass’s accessible fields and methods, subject to Java’s access and member inheritance rules. However, Java does not allow a class to extend more than one class directly.

Let’s consider a simple example to understand why Java does not support multiple inheritance through classes.

Consider a scenario as shown in the below figure. In this scenario, there are three classes: A, B, and C. Class C extends two parent classes, such as class A and class B. Assume that class A and class B both have the same method signature msg() (say) with different implementations.

Diagram showing why Java does not support multiple inheritance through classes.

According to inheritance concepts, both methods will inherit to class C. If we construct an object of class C and call the msg() method using the child class object reference, which msg() method will be invoked? (i.e., which copy of the msg() method will be available in class C?)

In this situation, the Java compiler will not be able to determine which of the methods in the parent class is to be called. Hence, it will create ambiguity and confusion in calling the method msg() from the A or B class.

Since the compile-time error is better than a runtime error, Java will give a compile-time error if we extend more than one class. So, whether you have the same or different method signature, you will still get a compile-time error.

Let us understand programmatically how Java does not support multiple inheritance.

Example 1: Java Prevents Multiple Inheritance Through Classes

class A {
    void msg() {
        System.out.println("Hello Java");
    }
}
class B {
    void msg() {
        System.out.println("Welcome you in the world of programming.");
    }
}

// Invalid in Java: a class cannot extend two classes
class C extends A, B {
    public static void main(String[] args) {
        C obj = new C();
        obj.msg(); // Which msg() method would be called?
    }
}

Output:

Compile-time error

As you can see in the above example, multiple inheritance leads to confusion when two superclasses have a method with the same name but different implementations.

Since Java supports single inheritance through classes, which means a class can extend only one superclass at a time. To reduce the ambiguity, complexity, and confusion, Java does not support multiple inheritance directly through classes.

Multiple Inheritance Using Interfaces in Java


We can achieve the functionality of multiple inheritance in Java by using interfaces. A class cannot extend more than one class, but a class can implement multiple interfaces simultaneously.

In Java, an interface can declare abstract methods without implementations. It can also provide implementations through default and static methods. Therefore, a class can inherit behavior from interfaces that define default methods.

If two interfaces provide default methods with the same signature, the implementing class must resolve the conflict by overriding the method, unless a more specific inherited implementation takes precedence under Java’s method-resolution rules.

Thus, Java supports multiple inheritance through interfaces while providing rules to handle potential conflicts between inherited default methods.

Let’s write a simple program to demonstrate multiple inheritance in Java using interfaces.

Example 2: Multiple Inheritance in Java Using Interfaces

// Java program to demonstrate multiple inheritance through interfaces.
interface Printable {
    // Interface methods are public and abstract by default.
    void print();
}
interface Showable {
    void show();
}
public class Test implements Printable, Showable {
    // Implement the abstract methods of both interfaces.
    @Override
    public void print() {
        System.out.println("Hello");
    }
    @Override
    public void show() {
        System.out.println("Java");
    }
    public static void main(String[] args) {
        Test t = new Test();
        t.print();
        t.show();
    }
}

Output:

Hello
Java

In this example:

  • We have created two interfaces named Printable and Showable.
  • Inside these interfaces, we have declared two abstract methods, print() and show(), without providing their implementations.
  • A class named Test implements both interfaces and provides implementations for both methods.
  • Inside the main() method, we have created an object of the Test class and called the print() and show() methods using the reference variable t.

Thus, Java allows a class to implement multiple interfaces, demonstrating multiple inheritance of type through interfaces. If you find this program difficult to understand, don’t worry. You will understand it more easily when you learn about interfaces in Java.

Example 3: Implementing Multiple Inheritance Using Interfaces

// Java program to demonstrate multiple inheritance using interfaces.
interface Father {
    void msg1();
}
interface Mother {
    void msg2();
}

class Child1 implements Father, Mother {
    @Override
    public void msg1() {
        System.out.println("Every father loves his child in the world.");
    }
    @Override
    public void msg2() {
        System.out.println("Every mother loves his child in the world.");
    }
}

class Child2 implements Father, Mother {
    @Override
    public void msg1() {
        System.out.println("I love my father very much.");
    }
    @Override
    public void msg2() {
        System.out.println("I love my mother very much.");
    }
}

public class Execution {
    public static void main(String[] args) {
        Child1 ch1 = new Child1();
        ch1.msg1();
        ch1.msg2();

        Child2 ch2 = new Child2();
        ch2.msg1();
        ch2.msg2();
    }
}

Output:

Every father loves his child in the world.
Every mother loves his child in the world.
I love my father very much.
I love my mother very much.

In this example:

  • The interfaces Father and Mother declare the abstract methods msg1() and msg2() without implementation, defining distinct functional contracts.
  • Both Child1 and Child2 use the implements Father, Mother clause to fulfill multiple type contracts simultaneously without causing multiple-inheritance conflicts.
  • Each child class provides its own unique logic for both inherited methods, demonstrating polymorphism and code customization per subclass.
  • The Execution class creates independent instances of Child1 and Child2, invoking their respective methods via standard object references.
  • Adding the @Override annotation above each method ensures compile-time checks against signature typos or contract mismatches.

Multiple Inheritance Conflict with Default Methods


When two interfaces provide default methods with the identical signature, a class implementing both interfaces faces a method collision known as the diamond problem. The Java compiler refuses to guess which implementation to invoke and forces an explicit resolution.

Example 4: The Diamond Problem with Default Methods

Here is how the Father and Mother interfaces handle a shared default method advice() and how the child classes resolve it:

// Interface 1 with a default implementation
interface Father {
    default void advice() {
        System.out.println("Father's advice: Work hard and stay disciplined.");
    }
}
// Interface 2 with the same default method signature
interface Mother {
    default void advice() {
        System.out.println("Mother's advice: Be kind and stay patient.");
    }
}

// Case 1: Selecting a specific parent interface implementation
class Child1 implements Father, Mother {
    @Override
    public void advice() {
        // Explicitly invokes Father's default implementation
        Father.super.advice();
    }
}

// Case 2: Combining both or providing a custom implementation
class Child2 implements Father, Mother {
    @Override
    public void advice() {
        // Invokes both implementations sequentially
        Father.super.advice();
        Mother.super.advice();
        System.out.println("Child2's takeaway: Balance hard work with kindness.");
    }
}

public class Execution {
    public static void main(String[] args) {
        Child1 ch1 = new Child1();
        System.out.println("--- Child 1 Advice ---");
        ch1.advice();

        Child2 ch2 = new Child2();
        System.out.println("\n--- Child 2 Advice ---");
        ch2.advice();
    }
}

Output:

--- Child 1 Advice ---
Father's advice: Work hard and stay disciplined.

--- Child 2 Advice ---
Father's advice: Work hard and stay disciplined.
Mother's advice: Be kind and stay patient.
Child2's takeaway: Balance hard work with kindness.

Key Technical Rules for Resolution

  1. You Must Choose: When two parent interfaces offer the exact same default method, Java gets confused and refuses to guess. You are required to write the method inside your child class to settle the conflict, or the code won’t compile.
  2. Call a Specific Parent: If you want to reuse one parent’s ready-made logic, point directly to it using InterfaceName.super.methodName().
  3. Write Your Own Rules: You aren’t forced to use either parent’s code. You can ignore both implementations completely and write entirely new code from scratch inside the child class.

Java Multiple Inheritance MCQ Quiz

1. What happens when a class inherits a concrete method from its superclass and a default method with the exact same signature from an interface?
A. A compile-time error occurs due to method collision.
B. The superclass method always wins and takes precedence.
C. The interface default method overrides the superclass method.
D. The program compiles but throws an IncompatibleClassChangeError at runtime.
The superclass method always wins and takes precedence.
Explanation: Under the Java Language Specification (JLS) method-resolution rules, any concrete method inherited from a superclass always overrides and takes precedence over default methods declared in any implemented interface.
2. Interface B extends Interface A. Both define a default method void display(). If class C implements B, which version of display() does C inherit?
A. A’s implementation because it is the base interface.
B. A compile-time conflict occurs requiring C to override display().
C. B’s implementation because it is the more specific sub-interface.
D. Neither; display() becomes abstract in class C.
B’s implementation because it is the more specific sub-interface.
Explanation:If an interface extends another and overrides a default method, the method from the more specific sub-interface takes precedence automatically without needing manual disambiguation.
3. Two unrelated interfaces X and Y both declare default void log(). Class App implements both X and Y without overriding log(). What is the result?
A. The code compiles, and X.log() is called by default.
B. The code compiles, but calling app.log() throws an AmbiguousMethodException.
C. A compile-time error occurs stating that types X and Y are unrelated and both define log().
D. The compiler selects the method from the interface listed first in the implements clause.
A compile-time error occurs stating that types X and Y are unrelated and both define log().
Explanation: When two unrelated interfaces define default methods with the exact same signature, Java refuses to guess. The compiler causes an error requiring the implementing class to explicitly override the method.
4. Which syntax is valid inside a class method to explicitly call a default method defined in an implemented interface named Printer?
A. super.Printer.print();
B. Printer.super.print();
C. Printer.this.print();
D. ((Printer) super).print();
Printer.super.print();
Explanation: Java uses the qualified super syntax <InterfaceName>.super.<methodName>() to disambiguate and directly invoke a specific parent interface’s default method implementation.
5. Interface Alpha defines default void run() { … }. Interface Beta re-declares void run(); as an abstract method. Class Delta implements both Alpha and Beta without providing a body for run(). What happens?
A. Delta compiles successfully and inherits Alpha’s default implementation.
B. A compile-time error occurs because an explicit abstract declaration re-abstracts the method, forcing Delta to implement it.
C. Delta compiles, but calling run() results in an AbstractMethodError at runtime.
D. Delta inherits Alpha’s method only if Alpha is listed before Beta in the implements clause.
A compile-time error occurs because an explicit abstract declaration re-abstracts the method, forcing Delta to implement it.
Explanation: If one interface re-declares a method as abstract, it “re-abstracts” the method contract across the combined hierarchy. The implementing class cannot rely on the sibling interface’s default body and must provide a concrete implementation (or be declared abstract).

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.