Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q14 of 15
Java - Interfaces
Identify the error in the following code snippet:
interface X {
    default void display() {
        System.out.println("X display");
    }
}

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

class Z implements X, Y {
    public void display() {
        // ???
    }
}

What should be done inside class Z's display() method to fix the error?
ARemove one interface from implements list
BCall <code>X.super.display()</code> or <code>Y.super.display()</code> to resolve ambiguity
CMake display() method abstract in class Z
DNo changes needed; code compiles fine
Step-by-Step Solution
Solution:
  1. Step 1: Understand diamond problem with default methods

    Class Z implements two interfaces X and Y, both having default display() methods, causing ambiguity.
  2. Step 2: Resolve ambiguity by overriding and calling specific interface method

    Class Z must override display() and explicitly call one interface's default method using X.super.display() or Y.super.display().
  3. Final Answer:

    Call X.super.display() or Y.super.display() to resolve ambiguity -> Option B
  4. Quick Check:

    Explicit super call fixes default method conflict [OK]
Quick Trick: Use InterfaceName.super.method() to fix default method conflicts [OK]
Common Mistakes:
  • Ignoring ambiguity and expecting code to compile
  • Trying to remove interfaces instead of overriding
  • Making method abstract in a concrete class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes