Multiple Inheritance of Default Methods
What you'll learn: How Java resolves conflicts when a class implements multiple interfaces that declare the same default method.
The Diamond Problem Returns
When a class implements multiple interfaces, each with a default method of the same signature, Java faces a classic "diamond problem"—which version should the class inherit? Unlike with abstract methods (where you simply provide one implementation), default methods already have bodies, so the compiler needs clear resolution rules.
The Conflict and Java's Rule
If two or more interfaces provide default methods with identical signatures, the class must explicitly override the method. Java won't guess which default to use—you must choose.
interface Flyable {
default void move() {
System.out.println("Flying through the air");
}
}
interface Swimmable {
default void move() {
System.out.println("Swimming through water");
}
}
class Duck implements Flyable, Swimmable {
// Compiler error if you don't override move()!
@Override
public void move() {
// Option 1: Pick one default
Flyable.super.move();
// Option 2: Custom implementation
// System.out.println("Waddling on land");
}
}
Why This Design?
Think of it like receiving contradictory instructions from two managers. Java refuses to make an arbitrary choice for you—you must consciously decide which behavior (or combination) your class needs.
You can call a specific interface's default using InterfaceName.super.methodName(), or provide entirely new logic.
Key Takeaway: When multiple interfaces provide the same default method, the implementing class must override it to resolve the ambiguity—Java enforces explicit conflict resolution.