The keyword that can be used to prevent a class from being extended in Java is final. Declaring a class as final, as in public final class Utility, prevents any other class from declaring it as a superclass.
This restriction is useful when a class’s design or security assumptions depend on preventing subclass customization. It also communicates that the class’s behavior is intended to be complete. Common Java library examples include String and wrapper classes such as Integer, which are final classes.
The same keyword has related but distinct uses. A final method cannot be overridden by a subclass, while a final variable or reference can be assigned only once. A final reference does not automatically make the referenced object immutable; the object’s internal state may still change.
Java’s sealed keyword is a common modern distraction. Introduced as a permanent language feature in Java 17, sealed classes restrict inheritance to a declared list of permitted subclasses, but they do not prohibit extension altogether. abstract and static likewise serve different purposes and do not prevent subclassing.