final Class and final Method in Java
Fast recall
Quick revision
Core answer
final class cannot be subclassed. A final instance method can be inherited but cannot be overridden, and a final static method cannot be hidden. These rules can preserve a design invariant, but final alone does not make a class immutable or secure; its fields and exposed mutable objects still matter.Understanding of final in its three forms — variable, method, class — and the design intent behind preventing inheritance or overriding
Only knowing final variable (constant) and forgetting final class and final method exist, or not understanding why you'd prevent inheritance
Mention that String and wrappers are final for immutability/security, and that final methods enable inlining by avoiding virtual dispatch
Interview-ready guide
Interview answer
A final class cannot be subclassed, and a final instance method cannot be overridden. A final static method likewise cannot be hidden in a subclass. For example, a subclass may inherit a final validate() method but cannot replace that signature; other non-final methods can still be overridden.
Final is an inheritance restriction, not an immutability or security guarantee. StringBuilder is final and still mutable. A genuinely immutable class must also protect its internal state and mutable values crossing its API. The JVM may optimize calls when it has enough evidence, but Java does not promise that marking a method final creates a particular inlining result.
final Class and final Method in Java
Direct Answer
A final class cannot be subclassed. A final instance method can be inherited but cannot be overridden, and a final static method cannot be hidden. These rules can preserve a design invariant, but final alone does not make a class immutable or secure; its fields and exposed mutable objects still matter.
Code Example
public final class StringChecks {
private StringChecks() { }
public static boolean isBlank(String value) {
return value == null || value.isBlank();
}
}
A private constructor is the important rule preventing instances of this utility class; final also states that it cannot be subclassed. Utility classes do not all have to use this exact shape, and java.util.Collections itself is not declared final.
Common Mistakes & Pitfalls
- Equating final with immutable:
StringBuilderis a final mutable class. - Saying final fields make every referenced object immutable: the reference cannot be reassigned, but the object may change.
- Promising security: blocking subclasses may help one design invariant but does not secure the whole API.
- Promising inlining: optimization is a JVM decision, not a language guarantee.
- Confusing override and overload: final blocks an overriding signature, not every new method with that name.
Check your understanding
Answer these without looking back at the article.
- Why is the String class final in Java?
- What is the difference between final method and private method?
- Can a final class implement an interface?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.