ProgrammingHardProduct-company placement · Java

Which line fails to compile?

Which line fails to compile?
class P { final void f(int x) { } }
class C extends P {
    void f(long x) { }   // L1
    void f(int y)  { }   // L2
}
  1. A. L1 only
  2. B. Both
  3. C. Neither
  4. D. L2 only

Correct answer: D. L2 only

final forbids overriding f(int); renaming the parameter does not change the signature. f(long) is a different signature, a legal overload.

More OOP with C++ and Java questions

Exams that ask this topic