Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Java - Interfaces
Consider this code:
interface X {
    default void show() { System.out.println("X show"); }
}
interface Y {
    default void show() { System.out.println("Y show"); }
}
class Z implements X, Y {
    public void show() {
        X.super.show();
        Y.super.show();
    }
}
public class Test {
    public static void main(String[] args) {
        new Z().show();
    }
}

What is the output?
ARuntime error
BCompilation error due to duplicate default methods
CX show Y show
DY show X show
Step-by-Step Solution
Solution:
  1. Step 1: Understand default method conflict resolution

    Class Z overrides show() and calls both interface default methods explicitly.
  2. Step 2: Trace method calls

    Calling new Z().show() prints "X show" then "Y show" in order.
  3. Final Answer:

    X show Y show -> Option C
  4. Quick Check:

    Explicit calls to interface default methods resolve conflicts = D [OK]
Quick Trick: Use InterfaceName.super.method() to call specific default methods [OK]
Common Mistakes:
  • Expecting compile error on default method conflict
  • Ignoring explicit calls to interface methods
  • Confusing output order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes