If we need to restrict access to classes and tired members, such as not allowing the outside class to access the data in this class, or restrict the scope of data, you can use the class member modifier. Common class member Modifiers are: public, protected, private, final, and so on.
Public:
Decorated objects: classes, member variables, member methods.
Limitations: 1. When the class is modified, it indicates that the class can be accessed or applied by all other classes;
2. When modifying a member variable or member method, the decorated member can be accessed not only by itself, but also by other classes.
Protected
Decorated objects: Mainly member variables, member methods
Restriction: When modifying a member, it indicates that the decorated member is a protected member, and in addition to being accessed by the class itself, it can be accessed by subclasses of the class and other classes in the same package.
Private
Decorated objects: Mainly member variables, member methods.
Restriction: When a member is decorated, it is stated that the decorated member cannot access any other class (including the subclass of the Class) by accessing the member only by its own amount.
Defaults----Default
If a class member is not modified by any of the public, protected, or private, default is defaulted, stating that the member can be accessed directly by the class itself and other classes under the same package as the class.
Summarize:
Final
Decorated objects: classes, member variables, member methods, local variables, parameter variables
Restriction: When 1.final modifies a class, it indicates that the class is the final class, and that the class does not want to inherit other classes from itself and does not want to handle the subclasses.
Declaring a class as final can improve efficiency and, of course, benefit and disadvantage, which is not conducive to the extension of the class, so the programmer will need to make a proper measurement when declaring.
Note: The class declared as final, all methods will default to the final method.
2.final Decorated member method
Consider using the final method in two cases: 1) to prevent any inherited class from altering the original purpose of the method.
2) Efficiency of program execution. The compiler embeds the contents of the final method directly into the calling method, eliminating the tedious process of method invocation, saving memory overhead and improving efficiency. The same benefits also have disadvantages, and in the final method the method body is very large, this method will cause the program bloated.
It is usually only considered that the method is set to final only if the code of the method is rarely or explicitly required to prevent the method from being overwritten.
Note: The private method cannot be accessed because the private method defaults to the final method.
3.final when modifying member variables
1) Final modification of the basic data type: Once the data is initialized, it cannot be modified, and the data becomes constant;
2) Final modifies a variable of a reference type that cannot be pointed to another object after it is initialized. Note, however, that the value of the object to which the reference is pointing can be changed.
4.final modifier Parameters
Java allows the parameter to be set to final, which means that within the method, we cannot change what the parameter application points to.
Abstract
Decorated objects: classes, methods.
Restriction: 1.abstract modifies a class to indicate that the class is abstract (cannot be instantiated and can only be used as the parent class of other classes)
2.abstract decoration method, indicates the method when the abstract method (only the method's return value type, method name, parameter list, no method body)
Abstract classes can contain abstract methods.
Java foundation----class member modifiers