ProgrammingHardTCS NQT · Infosys pattern

Which statement is correct?

Which statement is correct?
final class Immutable {
    void f() { }
}
class Sub extends Immutable {
}
  1. A. It compiles only if Sub also declares itself final
  2. B. It fails to compile: Immutable is declared final, so it cannot be extended at all
  3. C. It fails to compile because f() must be declared final too
  4. D. It compiles fine; final only prevents overriding f(), not subclassing Immutable

Correct answer: B. It fails to compile: Immutable is declared final, so it cannot be extended at all

A class-level `final` modifier forbids any subclassing whatsoever — it is stronger than marking individual methods final. `class Sub extends Immutable` is rejected at compile time regardless of what Sub does with f().

More OOP with C++ and Java questions

Exams that ask this topic