Second, Java object-oriented (11) _final modifier

2018-05-02

 

final modifier

 

Why is final modifier needed?

  The biggest disadvantage of inheritance relationship is that it destroys encapsulation: subclasses can access the implementation details of the superclass, and can modify the details in the form of method overriding.

---------------------------------------------------------------------------------------------------

 

There is no precedence relationship between multiple modifiers. public static final /public final static /final static public These are all correct.

-------------------------------------------------------------------------------------------------------------------

 

The meaning of final itself is final and immutable.

final can decorate non-abstract classes, non-abstract methods and variables . Note: The constructor cannot be final modified , because the constructor cannot be inherited (the constructor name must be the same as the class name, so it cannot be inherited), so the constructor itself is final

 

Final book definition:

1. Final can modify variables. After a variable modified by final is assigned an initial value, it cannot be reassigned

2. Final can modify methods, and methods modified by final are overridden

3. Final can modify a class, and a class modified by final cannot derive subclasses

In general, the value cannot be changed, the method cannot be overridden, and the class cannot be inherited, which is equivalent to the final version.

-----------------------------------------------------------------------------------------------

 

1. Variables modified by final: The final variable is a constant, which can only be assigned once and cannot be assigned again. Features: 1.  Final variables must display the specified initial value , and the system will not initialize the final field.
    

  And can only be initialized in the following three ways:

    1) Initialize when defining

    2) Specify initial values ​​for final instance variables in non-static initializer blocks

    3) Specify the initial value in the constructor


2. Once a final variable is assigned an initial value, it cannot be reassigned.
3. Constant name specification: The constant name conforms to the identifier, and the words are all capitalized. If it consists of multiple words, use underscores to separate the words. For example: final int MAX_VALUE = 6;

 

Global static variables: variables that are modified with public, static, and final at the same time. It can be called directly using the class name.

-------------------------------------------------------------------------------

Interview question: Does final modify a constant of a reference type mean that the address of the variable's reference cannot be changed or the content of the reference address cannot be changed?
  final modifies the variable of the basic type: it means that the value of the variable cannot be changed, that is, the constant of the reference type cannot be reassigned with the " = " sign.
  final modifies the constant of the reference type: it means that the address of the reference of the variable cannot be changed, not that the content of the reference address cannot be changed.

-----------------------------------------------------------------------------------------

final is the only modifier that can modify local variables
Local inner classes can only access final modified local variables
----------------------------- --------
When to use constants?

     When in a program, common data needs to be used in multiple places, and the data will not change, we specifically define global variables.

   Generally in development, we will define a constant class, which is specially used to store data.

 

----------------------------------------------------------------------------------------------------------------------------

 

Second, the final modified method: the final method, the method cannot be overridden by subclasses.


When does a method need to be finalized?
          1): The same algorithm skeleton provided in the parent class, subclasses are not allowed to be modified by method overriding, at this time, use final modification. Template method design pattern   
          2): The method (initialization method) called in the constructor, this Usually use final. Modification
Note: The method modified by fina can be called by subclasses, but cannot be overridden.

------------------------------------------------------------------------------------------------------------------------------

 

3. Final modified class: represents the final class, which can no longer have subclasses

 A class can be made final as long as one of the following conditions is met

  1) This class is not specifically designed for inheritance

  2) For security reasons, the implementation details of the class are not allowed to be changed

  3) This class will not be extended

 

Interview question: List 5 built-in classes in Java that use final modification

java里final修饰的类有很多,八大基本数据类型类和String类.

 

参考:https://www.cnblogs.com/JJ-Chen/p/4672695.html

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325209530&siteId=291194637