Multiple Inheritance in Java
Why Java doesn't support it and how
it's achieved using Interfaces
Why Java Doesn't Support Multiple
Inheritance
1. Diamond Problem:
• Ambiguity arises if two parent classes have the
same method.
• Compiler cannot decide which method to inherit.
2. Simplicity & Maintainability:
• Avoids complexity in the object model.
• Makes code easier to understand and maintain.
How Java Achieves Multiple
Inheritance
• Java supports multiple inheritance using
Interfaces.
• A class can implement multiple interfaces.
• Interfaces provide multiple inheritance of type
(not implementation).
Example: Multiple Inheritance with
Interfaces
interface Printable { void print(); }
interface Showable { void show(); }
class Demo implements Printable, Showable {
public void print() { [Link]("Printing..."); }
public void show() { [Link]("Showing..."); }
}
Output:
Printing...
Showing...