ProgrammingHardProduct-company placement · Java

Which line fails to compile?

Which line fails to compile?
class P { Number get() { return 1; }  int size() { return 0; } }
class C extends P {
    Integer get() { return 2; }   // L1
    long size()   { return 0; }   // L2
}
  1. A. L2 only
  2. B. L1 only
  3. C. Both L1 and L2
  4. D. Neither

Correct answer: A. L2 only

Covariant returns allow a subtype of a reference return type (Integer for Number). For primitive returns the type must match exactly, so long size() clashes with int size().

More OOP with C++ and Java questions

Exams that ask this topic