The final keyword can be used to modify classes, methods, and variables. Each has a different.
A, the Modified class (Class).
1. The class cannot be inherited.
2. The methods in the class are not overwritten, so the default is final.
3, use: when designing a class, if the class does not need to have subclasses, does not have to be extended, the implementation details of the class are not allowed to be changed, then the design of the final class
B. Modification Method
1, the method can be inherited, but cannot be overwritten.
2. Purpose: A class does not allow subclasses to overwrite the method, then it is modified with final
3, the Advantage: can prevent inheriting its subclass to modify the meaning and implementation of the method, more efficient, the compiler encountered the call fianal method into the embedded mechanism, improve the efficiency of execution.
4. Note: Private member methods in the parent class cannot be overridden by the quilt class, so the private method is final by default (you can view the compiled class file)
C, modifier variables (variable)
1. After final modification, it becomes constant. There are three types of static variables, instance variables, and local variables .
2, characteristics: You can first declare, do not give the initial value, this is called the final blank. However, it must be initialized before use. Once assigned, it can no longer be changed.
D, modifier parameters (arguments)
1. When the parameter is modified with final, the parameter can be read, but it cannot be modified.
Second, abstract class && interface
A, abstract class
1. Abstract Method: A method that does not contain a method body, which is implemented by inheriting its subclasses according to requirements, and must be modified with an abstract. In addition, the method must be visible to the child class, so add only public or protected!.
2. abstract class: A class that contains at least one abstract method (which can contain non-abstract methods). Must be modified with an abstract.
3. Features: Abstract classes cannot be instantiated (that is, they cannot be new); Classes containing abstract methods must be declared as abstract classes, and a class can be instantiated only if all the methods in the abstract class are implemented, or it is still an abstract class.
B, Interface (interface)
1.Interface Definition: A collection of abstract methods and constant values (all methods are abstract methods). It is a special kind of abstract class (a Xxx.class file is generated and the compiler automaticallyInterface KeywordsBefore addingAbstract)。
2.features:
A, in the interfaceConstantsBy default, allPublic static FinalTo be modified (if nothing is written,the compiler will automatically help you write)。
b, in the interfaceMethodBy default, allPublic Abstract(If you don't write anything, the compiler will automatically write it for you).
C, you can use extends to inherit the existing interface.
D, a class, can be implemented with implementsone or more interfacesIn theAllMethod (including the interface'smethods in the parent interface)。
E, an abstract class that can be implements implemented in the interfacePartial methods
F, a class that can extends a parent class while implements multiple interfaces, but the extends keyword must precede the Implements keyword.
Go from blog: blog.csdn.com/whuslei
Summary of final modifiers in Java