ProgrammingHardWipro · Cognizant technical pattern

What is printed?

What is printed?
interface I { default String who() { return "I"; } }
class P { public String who() { return "P"; } }
class C extends P implements I { }
// in main:
System.out.print(new C().who());
  1. A. I
  2. B. Compilation error: conflicting who()
  3. C. PI
  4. D. P

Correct answer: D. P

Rule: “class wins”. A method inherited from a superclass takes priority over an interface default method with the same signature, so there is no conflict and P’s version runs. A conflict error arises only between two interface defaults.

More OOP with C++ and Java questions

Exams that ask this topic