ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

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

Correct answer: C. P

When a class inherits a method from its superclass and a default method from an interface, the class method wins ("class wins" rule), so no override is needed and P's n() runs. A conflict only arises between two interfaces' defaults.

More OOP with C++ and Java questions

Exams that ask this topic