Course contentsShow
← Java
Lesson 499 of 2,87012. Abstract Classes and InterfacesFree lesson

Conflicting Default Methods

Explore the diamond problem with default methods and how to resolve conflicts using explicit override.

Conflicting Default Methods

What you'll learn: How Java handles and resolves conflicts when a class implements multiple interfaces that provide the same default method.


The Diamond Problem with Default Methods

When a class implements two interfaces that both declare a default method with the same signature, Java encounters a conflict—it doesn't know which implementation to use. This is the "diamond problem" adapted to interfaces.

Unlike inheriting from multiple classes (which Java prohibits), implementing multiple interfaces is allowed. But when those interfaces provide conflicting defaults, the compiler forces you to resolve the ambiguity explicitly.

The Conflict Scenario

interface Printer {
    default void display() {
        System.out.println("Printer display");
    }
}

interface Scanner {
    default void display() {
        System.out.println("Scanner display");
    }
}

// This will NOT compile without explicit resolution
class MultiFunctionDevice implements Printer, Scanner {
    // Error: inherits unrelated defaults for display()
}

Resolving the Conflict

You must override the conflicting method and explicitly choose which default to use (or provide your own implementation):

class MultiFunctionDevice implements Printer, Scanner {
    @Override
    public void display() {
        // Choose one explicitly using InterfaceName.super.methodName()
        Printer.super.display();
        // Or call both:
        // Scanner.super.display();
        // Or provide completely new behavior:
        // System.out.println("Custom display");
    }
}

The syntax Printer.super.display() explicitly invokes the default method from the Printer interface.

Why This Matters

Without explicit resolution, the compiler cannot determine programmer intent, preventing subtle runtime bugs. Java enforces clarity at compile time.

Key Takeaway: When implementing multiple interfaces with the same default method, you must override the method and use InterfaceName.super.methodName() to explicitly resolve the conflict or provide your own implementation.