Final:
Disadvantages of inheritance: it breaks the code encapsulation, and the appearance of final makes up for this drawback.
Final keywords:
1-final is a modifier that can be used to modify classes, methods, and variables;
2-The final modified class cannot be inherited
3-The final modifier method cannot be overwritten.
4-The variable modified by final is a constant, which can only be assigned once and must be assigned a value during definition.
Rule: www.2cto.com
The defined final constant must be expressed in uppercase letters and separated by underscores.
Instance 1:
Class Father01 {
Final String FATHER01_NAME = "James ";
Final int FATHER01_AGE = 20;
Final void show (){
// Age = 9; // Error !!! Age is defined as final. It has been assigned a value and cannot be assigned a value.
System. out. println ("name:" + FATHER01_NAME );
}
}
Final class Father02 {
}
Class Son01 extends Father01 {
// Public void show () {// Error !!! Methods defined by final cannot be overwritten.
//
//}
}
// Class Son02 extends Father02 {// Error !!! Classes defined by final cannot be inherited
//
//}