Multiple Inheritance Using Interface
Java does not support multiple inheritance using classes, but it fully supports multiple inheritance using interfaces. This allows a class to inherit multiple behaviors without the ambiguity problems that occur with class-based multiple inheritance. This is a very high-frequency interview topic.
What Is Multiple Inheritance?
Multiple inheritance means a class inherits from more than one parent.
Child → Parent1
→ Parent2
❌ Java does not allow this with classes
✔ Java allows this using interfaces
Why Java Disallows Multiple Inheritance with Classes
Diamond Problem (Ambiguity)
class A {
void show() { }
}
class B extends A {
void show() { }
}
class C extends A {
void show() { }
}
// class D extends B, C { } // ❌ Ambiguous
❓ Which show() should D inherit — from B or C?
👉 To avoid this ambiguity, Java prohibits multiple inheritance with classes.
How Interfaces Solve the Problem
- Interfaces do not carry state (no instance variables)
- Methods are abstract by default
- Implementing class provides the implementation
- No ambiguity in behavior resolution
Basic Syntax: Multiple Inheritance Using Interfaces
interface A {
void show();
}
interface B {
void display();
}
class C implements A, B {
public void show() {
System.out.println("Show from A");
}
public void display() {
System.out.println("Display from B");
}
}
✔ One class
✔ Multiple interfaces
✔ No ambiguity
Key Rules (Interview Favorites)
- A class can implement multiple interfaces
class Test implements A, B, C { }
- A class must implement all interface methods
- Otherwise, the class must be declared abstract
- Interfaces use implements, not extends (for classes)
- Interfaces can extend multiple interfaces
interface C extends A, B { }
Multiple Inheritance with Method Name Conflict (Java 7 and Below)
interface A {
void show();
}
interface B {
void show();
}
class C implements A, B {
public void show() {
System.out.println("Single implementation resolves conflict");
}
}
✔ No ambiguity
✔ One implementation satisfies both interfaces
Java 8+ Default Method Conflict (Important)
Problem
interface A {
default void show() {
System.out.println("A");
}
}
interface B {
default void show() {
System.out.println("B");
}
}
class C implements A, B {
// ❌ Compile-time error
}
❌ Ambiguous default methods
Solution: Override the Method
class C implements A, B {
@Override
public void show() {
A.super.show(); // or B.super.show()
}
}
✔ Explicit conflict resolution
✔ Fully controlled behavior
Interface vs Class in Multiple Inheritance
| Aspect | Interface | Class |
|---|---|---|
| Multiple inheritance | ✅ Yes | ❌ No |
| State (variables) | ❌ No | ✅ Yes |
| Ambiguity | ❌ No | ❌ Yes |
| Implementation | Provided by class | Inherited |
| Safety | High | Risky |
Real-World Example (Interview-Friendly)
interface Camera {
void takePhoto();
}
interface GPS {
void navigate();
}
class SmartPhone implements Camera, GPS {
public void takePhoto() {
System.out.println("Taking photo");
}
public void navigate() {
System.out.println("Navigating");
}
}
✔ One device
✔ Multiple capabilities
✔ Clean design
Why Multiple Inheritance via Interface Is Powerful
- Supports loose coupling
- Enables plug-and-play behavior
- Avoids ambiguity
- Forms the backbone of Java frameworks
Examples:
- JDBC (Connection, Statement)
- Collections (List, Set, Map)
- Spring interfaces
Common Beginner Mistakes
- Trying to extend multiple classes
- Forgetting to implement all interface methods
- Confusing extends vs implements
- Ignoring default method conflicts
- Overusing interfaces without need
Interview-Ready Answers
Short Answer
Java supports multiple inheritance using interfaces, not classes.
Detailed Answer
Java avoids multiple inheritance with classes to prevent ambiguity (diamond problem). However, it supports multiple inheritance using interfaces, where a class can implement multiple interfaces and provide its own implementation, ensuring clarity and flexibility.
Key Takeaway
Java achieves multiple inheritance safely through interfaces. This design delivers flexibility without ambiguity, making interfaces a cornerstone of robust Java architecture.