ProgrammingHardHCL · Tech Mahindra pattern

What happens when this is compiled?

What happens when this is compiled?
class A {
    final void m() { System.out.println("A"); }
}
class B extends A {
    void m() { System.out.println("B"); }
}
  1. A. Compiles; B.m overrides A.m
  2. B. Compile-time error: m() in A cannot be overridden
  3. C. Runtime error when B.m is invoked
  4. D. Compiles; B.m is treated as an overload

Correct answer: B. Compile-time error: m() in A cannot be overridden

A method marked final cannot be overridden by any subclass. B's attempt to redeclare m() with the same signature is rejected at compile time.

More OOP with C++ and Java questions

Exams that ask this topic