ProgrammingVery hardWipro Elite · TCS NQT Prime

What does new D().w() return?

What does new D().w() return?
interface A { default String w() { return "A"; } }
interface B extends A { default String w() { return "B"; } }
interface C extends A { }
class D implements B, C { }
  1. A. "A"
  2. B. Compile error: conflicting defaults
  3. C. Runtime AbstractMethodError
  4. D. "B"

Correct answer: D. "B"

B overrides A.w, and C merely inherits A.w, so there is no conflict: the more specific B.w wins.

More OOP with C++ and Java questions

Exams that ask this topic