The final Keyword for Classes & Methods
The final Keyword for Classes & Methods
So far in this tutorial you have learned how to extend classes and override methods to build flexible hierarchies. But sometimes flexibility is the wrong goal. Sometimes you want to say: "This class must never be subclassed" or "This method must never be replaced." Java gives you a single keyword for both purposes: final.
What final means on a method
When you mark a method final, no subclass can override it. The implementation you wrote is locked in place for the entire inheritance chain.
The withdrawal logic enforces a business rule: you cannot withdraw a negative amount or more than your balance. Making it final guarantees that no rogue subclass can bypass that rule.
final on a method when correct behaviour depends on that method doing exactly what it does — and a subclass overriding it would introduce bugs or security holes.
What final means on a class
When you mark an entire class final, it cannot be extended at all. Nobody can write class Foo extends YourFinalClass.
Because the class is final and all its fields are private final, an ImmutablePoint object is guaranteed to be immutable — its coordinates can never change after construction, and no subclass can sneak in mutable state.
Java's own final classes — String
String is the most famous example of a final class in the Java standard library. You cannot extend it:
Why did the Java designers make String final? Several reasons work together:
- Security. Many parts of the JVM and standard library receive a
Stringand trust its value (file paths, class names, passwords). IfStringwere extendable, a malicious subclass could overrideequalsortoStringto lie about its value after a security check. - Immutability.
Stringobjects are immutable — their internalchar[](orbyte[]in modern Java) never changes. Immutability is only guaranteed if subclasses cannot add mutable fields. - String interning & the constant pool. The JVM caches
Stringliterals. That optimisation is safe only because all strings behave identically; subclasses would break the guarantee.
Integer, Long, Double, and all the other primitive wrapper types are final for the same immutability and correctness reasons. So is Math.
final fields — a quick reminder
You have already seen final on fields (a variable that can only be assigned once). That is a separate — but related — use of the same keyword. All three uses share one philosophy: commit to a value or a behaviour and prevent it from changing.
When should you use final?
A useful mental checklist:
- Mark a method final when it implements a security rule, a protocol step, or an algorithm that subclasses must rely on but must not alter (e.g. a template-method skeleton that calls other overridable hooks, but whose orchestration is fixed).
- Mark a class final when the class represents a value type that must stay immutable (like
StringorInteger), or when the class is a utility class (likeMath) that should never be instantiated through a subclass. - Do not overuse final. Marking everything final makes your code harder to test (you cannot create test doubles) and harder to extend when requirements change.
final to prevent misuse, only to discover later that a legitimate use-case needs extension. Prefer designing with good access modifiers and encapsulation first. Reach for final when you have a concrete reason — not just as a habit.
Compile-time enforcement
final is checked entirely at compile time — there is no runtime overhead. If you try to extend a final class or override a final method, the compiler rejects the code immediately with a clear error message. This makes it a zero-cost safety net.
Summary
The final keyword on a method prevents any subclass from overriding that method, locking a specific behaviour in place. On a class it prevents the class from being extended at all, which is how Java guarantees that String, Integer, and similar types stay immutable and trustworthy. Use final deliberately: to enforce a contract, to preserve immutability, or to protect a security-critical implementation.