Multiple Inheritance in Java
What is Multiple Inheritance?
- Definition: Multiple inheritance is a feature of object-oriented programming where a class can inherit properties and methods from more than one parent class.
- Why It’s Useful:
- Promotes code reuse.
- Allows a class to inherit behavior from multiple sources.
Is Multiple Inheritance Supported in Java?
Java does not support multiple inheritance with classes to avoid ambiguity caused by the Diamond Problem. However, it supports multiple inheritance with interfaces.
1. Multiple Inheritance with Classes
Why Not Supported?
The Diamond Problem occurs when a class inherits from two classes that have a method with the same signature. Java avoids this problem by disallowing multiple inheritance with classes.
Example of Diamond Problem
class A {
void display() {
System.out.println("Class A");
}
}
class B {
void display() {
System.out.println("Class B");
}
}
// This is not allowed in Java
// class C extends A, B {
// // Ambiguity: Which display() method to use?
// }
In the above example, if C tried to inherit from both A and B, it would lead to ambiguity for the display() method.
2. Multiple Inheritance with Interfaces
Java allows multiple inheritance through interfaces because interfaces only declare methods without implementing them, resolving ambiguity.
Example: Multiple Inheritance with Interfaces
interface A {
void methodA();
}
interface B {
void methodB();
}
class C implements A, B {
@Override
public void methodA() {
System.out.println("Method A");
}
@Override
public void methodB() {
System.out.println("Method B");
}
}
public class Main {
public static void main(String[] args) {
C obj = new C();
obj.methodA(); // Output: Method A
obj.methodB(); // Output: Method B
}
}
3. Resolving Default Method Ambiguity in Interfaces
From Java 8 onwards, interfaces can have default methods. If two interfaces have default methods with the same name, the implementing class must override the method to resolve ambiguity.
Example: Resolving Default Method Conflict
interface A {
default void display() {
System.out.println("Display from Interface A");
}
}
interface B {
default void display() {
System.out.println("Display from Interface B");
}
}
class C implements A, B {
@Override
public void display() {
System.out.println("Resolving conflict in Class C");
}
}
public class Main {
public static void main(String[] args) {
C obj = new C();
obj.display(); // Output: Resolving conflict in Class C
}
}
4. Calling a Default Method of an Interface
If a class implements multiple interfaces with default methods, you can explicitly call a specific default method using the following syntax:
Example: Calling Default Methods
interface A {
default void display() {
System.out.println("Display from Interface A");
}
}
interface B {
default void display() {
System.out.println("Display from Interface B");
}
}
class C implements A, B {
@Override
public void display() {
A.super.display(); // Explicitly calling Interface A's default method
B.super.display(); // Explicitly calling Interface B's default method
System.out.println("Resolving conflict in Class C");
}
}
public class Main {
public static void main(String[] args) {
C obj = new C();
obj.display();
}
}
Output:
Display from Interface A Display from Interface B Resolving conflict in Class C
Key Differences: Classes vs Interfaces in Multiple Inheritance
| Feature | Classes | Interfaces |
|---|---|---|
| Multiple Inheritance | Not supported | Supported |
| Implementation | Methods are implemented. | Methods are declared (default methods from Java 8). |
| Ambiguity Resolution | Not applicable (prohibited). | Requires explicit override if conflicts exist. |
Conclusion
- Java does not support multiple inheritance with classes to avoid ambiguity and maintain simplicity.
- Multiple inheritance with interfaces is supported and widely used in Java.
- Use interfaces for achieving multiple inheritance and resolving ambiguity when working with default methods.