ProgrammingHardProduct-company placement · Java

What happens when C is compiled?

What happens when C is compiled?
class P { static void s() { } }
class C extends P {
    @Override static void s() { }
}
  1. A. Compile-time error: s() does not override a superclass method
  2. B. It compiles; C.s() overrides P.s()
  3. C. It compiles with a warning; C.s() hides P.s()
  4. D. Compile-time error: static methods cannot be redeclared

Correct answer: A. Compile-time error: s() does not override a superclass method

Static methods are hidden, never overridden. Without @Override the code compiles (hiding); the annotation makes the compiler check for a real override and fail.

More OOP with C++ and Java questions

Exams that ask this topic