In Java, inheritance is a powerful concept that allows one class to inherit properties and behavior from another. However, Java does not support multiple inheritance using classes due to ambiguity problems.
So how do we achieve multiple inheritance in Java?
The answer is simple: Interfaces.
In this guide, you will learn how multiple inheritance works using interfaces in Java, along with a clear example that is easy to understand and perfect for exams.
What is Multiple Inheritance?
Multiple inheritance means a class can inherit from more than one parent class.
Languages like C++ support it directly, but Java avoids it to prevent confusion known as the diamond problem.
Instead, Java uses interfaces to safely achieve multiple inheritance.
What is an Interface in Java?
An interface is a collection of abstract methods. A class implements an interface instead of extending it.
interface A {
void show();
}
A class can implement multiple interfaces, which allows multiple inheritance.
Multiple Inheritance using Interfaces
In Java, a class can implement more than one interface.
This allows the class to inherit behavior from multiple sources.
class MyClass implements Interface1, Interface2 {
// implementation
}
Example Program
Let’s understand this with a simple and clear example.
interface A {
void showA();
}
interface B {
void showB();
}
class C implements A, B {
public void showA() {
System.out.println("This is Interface A");
}
public void showB() {
System.out.println("This is Interface B");
}
void display() {
System.out.println("Multiple Inheritance Achieved");
}
}
public class Test {
public static void main(String[] args) {
C obj = new C();
obj.showA();
obj.showB();
obj.display();
}
}
Output
This is Interface A This is Interface B Multiple Inheritance Achieved
How This Works
- Interface A and B define methods
- Class C implements both interfaces
- C provides implementation for all methods
- Main method calls all functions
This is how Java safely supports multiple inheritance.
Why Java Uses Interfaces for Multiple Inheritance
- Avoids ambiguity (diamond problem)
- Keeps design simple
- Improves flexibility
- Supports loose coupling
Real-World Use Cases
- Combining multiple features (e.g., Printer + Scanner)
- Software frameworks
- API design
- Modular application development
Important Rules
- A class can implement multiple interfaces
- All methods must be implemented
- Interface methods are public by default
Common Mistakes
- Forgetting to implement all methods
- Using extends instead of implements
- Not declaring methods as public
FAQ
Does Java support multiple inheritance?
No, Java does not support it using classes but supports it using interfaces.
Why not multiple inheritance with classes?
Because of ambiguity problems like the diamond problem.
Can a class implement multiple interfaces?
Yes, a class can implement any number of interfaces.
Conclusion
Multiple inheritance in Java is achieved using interfaces, making the language safer and more structured. By implementing multiple interfaces, a class can inherit behavior from different sources without confusion.
Practice this concept with examples to fully understand how it works in real applications.

Comments
Post a Comment