Java object oriented basics _final

  • 2020-04-01 01:08:40
  • OfStack

final :
The drawback of inheritance: it breaks the encapsulation of the code, and the appearance of final makes up for it
Final keyword:
1-final is a modifier that modifies classes, methods, and variables.
Classes that are 2-final cannot be inherited
Methods that are 3-final cannot be overridden
A 4-final modifier is a constant that can only be assigned once and assigned to a value at definition

The rules :
Constants that are defined as final are capitalized and separated by an underscore _.
Example 1:
The class Father01 {
Final String FATHER01_NAME = "zhang SAN ";
Final int FATHER01_AGE = 20;

The final void show () {
/ / age = 9; / / error!!!!!! Age is defined as final, has been assigned at definition and cannot be assigned again
System.out.println(" name: "+ FATHER01_NAME);
}
}
Final class Father02 {
}
The class Son01 extends Father01 {
// public void show() {// error!! Methods defined by final cannot be overridden
//
/ /}
}
//class Son02 extends Father02{// error!! Classes defined by final cannot be inherited
//
/ /}

Related articles: