ProgrammingVery hardProduct company interview · GATE level

Which lines fail to compile?

Which lines fail to compile?
interface I {
    static String id() { return "I"; }
    default String name() { return "N"; }
}
class C implements I { }
// in main:
C c = new C();
String s1 = I.id();     // L1
String s2 = C.id();     // L2
String s3 = c.name();   // L3
String s4 = c.id();     // L4
  1. A. L4 only
  2. B. L2 only
  3. C. L2 and L4
  4. D. None of them

Correct answer: C. L2 and L4

Static methods of an interface are not inherited by implementing classes (unlike static methods of a superclass), so they can be called only as I.id(). C.id() and c.id() both fail. Default methods are inherited, so c.name() is fine.

More OOP with C++ and Java questions

Exams that ask this topic