ProgrammingVery hardProduct-company placement · Java

What happens when this is compiled?

What happens when this is compiled?
interface A { default void hello() { System.out.print("A"); } }
interface B extends A { void hello(); }
class C implements B { }
  1. A. It compiles; new C().hello() prints A
  2. B. Compile-time error: C must implement hello()
  3. C. Compile-time error in B: a default method cannot be redeclared abstract
  4. D. It compiles; calling hello() throws AbstractMethodError

Correct answer: B. Compile-time error: C must implement hello()

A sub-interface may re-abstract an inherited default method. B's hello() is abstract again, so the concrete class C must provide it.

More OOP with C++ and Java questions

Exams that ask this topic