ProgrammingHardWipro · Cognizant pattern

What happens?

What happens?
interface A { default String id() { return "A"; } }
interface B { default String id() { return "B"; } }
class D implements A, B { }
  1. A. It compiles, picking A's default because A is listed first
  2. B. Compilation error: D must override id() since neither default is more specific than the other
  3. C. It compiles, picking B's default because it is declared later
  4. D. It compiles and both defaults run, concatenated

Correct answer: B. Compilation error: D must override id() since neither default is more specific than the other

A and B are unrelated interfaces, so neither default method is more specific than the other (classic diamond with no common ancestor relation between them). Java refuses to guess and forces D to explicitly override id() itself (optionally delegating via A.super.id() or B.super.id()); the class fails to compile as written.

More OOP with C++ and Java questions

Exams that ask this topic