ProgrammingHardProduct-company MCQ pattern

Interfaces A and B both declare default void hello(). What happens with class C implements A, B { } ?

  1. A. B's hello is used because it is listed last
  2. B. It compiles, and calling hello() throws an exception
  3. C. A's hello is used because A is listed first
  4. D. Compilation error: C must override hello() (it may call A.super.hello() inside)

Correct answer: D. Compilation error: C must override hello() (it may call A.super.hello() inside)

When two interfaces supply the same default method, Java refuses to guess. C must override hello(), and may delegate to one of them with A.super.hello() or B.super.hello().

More OOP with C++ and Java questions

Exams that ask this topic