Preventing Inheritance for Members
We can mark classes and methods with the final
keyword to prevent them from being inherited.
Prevent Class Inheritance
Most of the classes in the java.lang package are defined as final so that they can not be inherited. Here is the definition of the String class :
public final class String{
//some methods omitted for readability's sake
}
Prevent Method Inheritance
We can mark the method with the final
keyword to prevent it from being inherited.