0
0
JavaHow-ToBeginner · 3 min read

How to Achieve Multiple Inheritance Using Interface in Java

In Java, multiple inheritance is achieved using interfaces because a class can implement multiple interfaces. Each interface can declare methods that the class must override, allowing the class to inherit behavior from multiple sources without the problems of multiple class inheritance.
📐

Syntax

To achieve multiple inheritance using interfaces, a class implements more than one interface separated by commas. Each interface declares method signatures that the class must provide implementations for.

  • interface InterfaceName { ... }: Defines an interface.
  • class ClassName implements Interface1, Interface2 { ... }: A class implementing multiple interfaces.
java
interface InterfaceA {
    void methodA();
}

interface InterfaceB {
    void methodB();
}

class MyClass implements InterfaceA, InterfaceB {
    public void methodA() {
        System.out.println("Method A implementation");
    }
    public void methodB() {
        System.out.println("Method B implementation");
    }
}
💻

Example

This example shows a class implementing two interfaces, each with one method. The class provides the required method implementations, demonstrating multiple inheritance of behavior.

java
interface Printable {
    void print();
}

interface Showable {
    void show();
}

class Document implements Printable, Showable {
    public void print() {
        System.out.println("Printing document...");
    }
    public void show() {
        System.out.println("Showing document...");
    }
}

public class Main {
    public static void main(String[] args) {
        Document doc = new Document();
        doc.print();
        doc.show();
    }
}
Output
Printing document... Showing document...
⚠️

Common Pitfalls

Common mistakes include:

  • Not implementing all methods from all interfaces, causing compilation errors.
  • Confusion when interfaces have default methods with the same signature; the class must override to resolve ambiguity.
  • Trying to extend multiple classes instead of implementing interfaces, which Java does not allow.

Example of conflicting default methods and how to fix it:

java
interface Interface1 {
    default void greet() {
        System.out.println("Hello from Interface1");
    }
}

interface Interface2 {
    default void greet() {
        System.out.println("Hello from Interface2");
    }
}

class MyClass implements Interface1, Interface2 {
    // Must override greet() to resolve conflict
    public void greet() {
        Interface1.super.greet(); // or Interface2.super.greet();
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.greet();
    }
}
Output
Hello from Interface1
📊

Quick Reference

  • Use implements keyword to inherit multiple interfaces.
  • Implement all abstract methods from all interfaces.
  • Override conflicting default methods explicitly.
  • Interfaces support multiple inheritance; classes do not.

Key Takeaways

Java supports multiple inheritance only through interfaces, not classes.
A class can implement multiple interfaces by separating them with commas.
All abstract methods from interfaces must be implemented in the class.
Conflicting default methods from interfaces require explicit overriding.
Interfaces provide a safe way to inherit multiple behaviors without ambiguity.