ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
class Base {
    final void secure() { System.out.print("Base.secure"); }
}
class Derived extends Base {
    // void secure() { System.out.print("Derived.secure"); } // line X
}
What happens if line X is uncommented?
  1. A. It compiles: Derived.secure overrides Base.secure
  2. B. Compilation error: cannot override the final method secure() in Base
  3. C. It compiles: both methods coexist as overloads
  4. D. It compiles, but Derived.secure is unreachable

Correct answer: B. Compilation error: cannot override the final method secure() in Base

A method marked `final` can never be overridden by any subclass; Derived attempting to redeclare secure() with the identical signature is a direct override attempt, which javac rejects with a compile-time error.

More OOP with C++ and Java questions

Exams that ask this topic