Final
According to the program context, the Java keyword final has "This is immutable" or "final State" meaning, it can modify the Non-abstract class, Non-abstract class member methods and variables. You may need to stop the change in terms of two understandings: design or efficiency.
The final class cannot be inherited, no subclasses, and the method in the final class is final by default.
The final method cannot be overridden by a quilt-class method, but can be inherited.
Final member variables represent constants that can only be assigned once, and values are not changed after assignment.
Final cannot be used to modify the construction method.
Note: The private member method of a parent class cannot be overridden by a quilt class method, so the private type method defaults to the final type.
1. Final class
The final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, and the default is final. When designing a class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change, and it is assured that the class will not be extended, then the final class is designed.
2. Final method
If a class does not allow its subclasses to overwrite a method, you can declare the method as the final method.
There are two reasons for using the final method:
First, the method is locked to prevent any inherited class from modifying its meaning and implementation.
Second, efficient. The compiler goes into the inline mechanism when it encounters a call to the final method, greatly improving execution efficiency.
For example:
public class Test1 {public
static void Main (string[] args) {
//TODO automatically generate method stub
} public
void F1 () {
Sy Stem.out.println ("F1");
}
Methods that cannot be overridden by a quilt class are public
final void F2 () {
System.out.println ("F2");
}
public void F3 () {
System.out.println ("F3");
}
private void F4 () {
System.out.println ("F4");
}
public class Test2 extends Test1 {public
void F1 () {
System.out.println ("Test1 parent method F1 overwritten!");
public static void Main (string[] args) {
Test2 t=new Test2 ();
T.f1 ();
T.f2 (); Invokes the final method T.f3 () inherited from the parent class
();//Calls methods inherited from the parent class
//t.f4 ();//call failed, cannot inherit from parent class
}