The final keyword prevents extension or overriding. You can apply it to classes, methods, or variables.
A final class cannot be extended. No other class can inherit from it.
public final class Constants {
public static final double PI = 3.14159;
}
Writing class MyConstants extends Constants causes a compile error.
A final method cannot be overridden. The parent locks down that behavior so no child can change it.
public class Account {
public final double getBalance() {
return this.balance;
}
}
You'd mark getBalance() as final if you need to guarantee that no subclass can tamper with how the balance is reported. This is common in security-sensitive code where consistent behavior matters.
The String class is a familiar example of a final class. You can't extend String in Java.