ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
interface A { default String hi() { return "A"; } }
interface B extends A { default String hi() { return "B"; } }
class C implements A, B { }
// in main:
System.out.print(new C().hi());
  1. A. B
  2. B. A
  3. C. Compile-time error: C inherits two defaults for hi()
  4. D. AB

Correct answer: A. B

There is no conflict because B is a subinterface of A and overrides its default: the most specific default (B's) wins. A conflict arises only between unrelated interfaces.

More OOP with C++ and Java questions

Exams that ask this topic