- Introduction to JVM Languages
- Vincent van der Leun
- 76字
- 2021-07-02 21:46:34
Final class modifier - locking a class
A class can be prefixed with the final non-access modifier to prevent the class from being inherited by another class:
public final class ThisClassCanNotBeOverriden {
}
The final keyword can be added before or after a class's access modifier, if present. By convention the access modifier is specified first, followed by non-access modifiers. If another class tries to inherit a final class, the compiler will refuse to compile the code.