After reading sun Xin's video, I made a small Summary of classes, methods, and variable modifiers in Java;
|
|
Public |
Private |
Protected |
Default)
|
|
Explanation:
|
Can be called in any class
|
It can be called only in the same type.
|
The same package can be called by subclasses (including subclasses of different packages;
|
All classes in the same package can be accessed, but if the subclass (not in the same package), it cannot be accessed.
|
|
Similar
|
Yes
|
Yes
|
Yes
|
Yes
|
|
Same package
|
Yes
|
|
Yes
|
Yes
|
|
Subclass
|
Yes
|
|
Yes
|
|
|
Versatility
|
Strong |
|
|
|
The final modification is used to ensure that the behavior of a function remains unchanged during the inheritance process. For example, the string class in Java cannot be inherited, but the final modification method cannot be overwritten; sometimes we add static in front of final. As you know, static modified variables belong to the class itself. In fact, static variables are used.
Final
Variable modification is to save space in the memory. Because every object has a copy of the variable without static modification, it is considered redundant, in fact, final is final, and we do not want it to be changed, so we should add static
In this way, the memory space can be saved. What's more, static is the final method, instead of being inherited in sub-classes;
Abstract methods are called abstract methods. abstract classes must be abstract classes. abstract classes cannot be instantiated because their behaviors are not determined. Therefore, we can declare a class as an abstract class to avoid any objects produced by this class;
The native method is a method that can be used by Java users, but cannot be written. This is a Java Local interface, which is not explained due to my experience;
All methods in the interface are public abstract. When no modifier is added, the default value is public.
Abstract;It is worth noting that when the method in interface is not fully implemented in the implementation class, the class is abstract. Of course, the above native, static, final, synchronized, Private, protected, and other modifiers; the default interface can only be used in one package, and the interface can have data members, and these members are public by default.
Static final static constants;
Synchronized synchronization is used in Java threads. When you see a thread, it is explained;
Hope you can give me some advice