Introduction to final, finalcutpro
About final
Final is used to modify methods and attributes to indicate special meanings. When modifying a method, the method cannot be overwritten. When modifying an attribute, the attribute cannot be changed. Here, the attribute is divided into objects and basic types. When modifying a basic type, the attribute cannot be assigned a value after the basic type is assigned, the modifier object indicates that this property cannot point to other objects (the reference remains unchanged), but the object to which it points can still be changed.
Since the final method cannot be rewritten, are there any mistakes in the following procedures?
class Base { private final void method() { System.out.println("In Base..."); }}class Sub extends Base { public void method() { System.out.println("In Sub..."); }}
Answer: Correct.
You can use final keywords to declare classes, member variables, and member methods. Once declared, the class, member variables, and member methods cannot be inherited, modified, or overwritten.
In this example, the base class method is private and invisible in the subclass. Therefore, the method with the same name in the subclass is no longer overwritten, which is equivalent to a new method name.
This situation is special. The book says that if a method in the subclass has the same method name and parameter signature as the method in its superclass, it means that the method in the subclass override in the superclass. This statement is not strict.
Additional tips:
The final method cannot be overwritten by the quilt class. The compiler transfers the final method to the embedded mechanism (directly inserts the method subject into the calling place), greatly improving the execution efficiency (about 50% ). Many methods in the API class, for example, the getClass method in the Object class is the final method.
Note: final cannot be used to modify the constructor.
References:
Http://www.cnblogs.com/hasse/p/5015475.html