ProgrammingHardIBM · Deloitte pattern

Which statement is correct?

Which statement is correct?
class Base {
    final void process() { System.out.print("Base.process"); }
}
class Sub extends Base {
    void process() { System.out.print("Sub.process"); }
}
  1. A. It compiles; Sub.process() overrides Base.process()
  2. B. It fails to compile: a final method cannot be overridden by a subclass
  3. C. It compiles only if Sub.process() also uses final
  4. D. It compiles, and calling process() on a Sub instance prints 'Sub.process'

Correct answer: B. It fails to compile: a final method cannot be overridden by a subclass

Marking a method final explicitly forbids any subclass from overriding it, guaranteeing its implementation and dispatch target can never change. Attempting to redeclare process() with the same signature in Sub is a compile-time error.

More OOP with C++ and Java questions

Exams that ask this topic