Difficulty: Easy
Correct Answer: Declare the class as final so that no other class can extend it.
Explanation:
Introduction / Context:
Sometimes you want to design a class that should not be extended, either to preserve security invariants, to keep behavior stable, or to simplify maintenance. Java provides a mechanism to prevent inheritance of a particular class. Interviewers ask this question to verify that you know how to control inheritance using the appropriate class level modifier.
Given Data / Assumptions:
Concept / Approach:
Java uses the final keyword to mark a class as non-inheritable. When you declare a class as final, for example public final class MyClass, the compiler prohibits any attempt to create a subclass using extends MyClass. This is similar to the way final prevents methods from being overridden and variables from being reassigned. The final keyword is the correct and intended way to signal that a class is closed for extension, while still allowing normal usage and instantiation of that class.
Step-by-Step Solution:
Step 1: Start with a normal class declaration such as class Account { ... } which other classes can extend.
Step 2: Add the final keyword to declare public final class Account { ... }.
Step 3: Attempt to declare class SpecialAccount extends Account; and observe that the compiler reports an error because Account is final.
Step 4: Note that you can still create instances of Account directly; only subclassing is blocked.
Step 5: Conclude that marking the class as final is the correct way to prevent inheritance.
Verification / Alternative check:
Testing this in a simple project confirms the rule. When you remove final from the class declaration, subclasses compile and work normally. When you add final back, the compiler rejects subclasses immediately. Many standard library classes, such as java.lang.String, are declared final, illustrating how final is used in practice to prevent unintended inheritance and to maintain class invariants.
Why Other Options Are Wrong:
Option B is incorrect because private is not allowed for top-level classes and does not globally disable inheritance; for nested classes it controls visibility, not subclassing in general. Option C is wrong because having no methods does not stop inheritance; subclasses can still be created and can add their own methods. Option D is incorrect since you cannot declare a top-level class inside a method; only local or anonymous classes can be declared inside a method and that does not solve the general inheritance restriction for a reusable type.
Common Pitfalls:
A common pitfall is trying to use constructors or access modifiers alone to prevent inheritance, which may not be sufficient if the class remains non-final. Another mistake is marking every class final by default, which can limit extensibility in frameworks where inheritance is expected. Designers should carefully choose which classes should be final, balancing the need for stability and security against the benefits of extensibility.
Final Answer:
To prevent a class from being inherited, you should declare the class as final so that no other class can extend it.
Discussion & Comments