ProgrammingVery hardProduct-company placement · Java

What happens when this is compiled?

What happens when this is compiled?
interface I { default void f() { System.out.print("I"); } }
abstract class A { public abstract void f(); }
class C extends A implements I { }
  1. A. It compiles; new C().f() prints I
  2. B. Compile-time error: A and I conflict
  3. C. Compile-time error: C must implement f()
  4. D. It compiles; calling f() throws AbstractMethodError

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

'Class wins': a method inherited from the superclass, even an abstract one, takes precedence over an interface default. A.f() is abstract, so the concrete class C must implement it.

More OOP with C++ and Java questions

Exams that ask this topic