Implementation of Java encapsulation

Use access control characters

Java uses "access control symbols" to control which properties or methods or even classes need to be encapsulated, and which properties or methods or even classes need to be exposed.
The four types of "access control symbols" in Java are private, default, protected, and public. They have different encapsulation properties. We should try our best to minimize access permissions to improve security.

Access modifier

Modifier Same class Same package Subclass All categories
private *
default * *
protected * * *
public * * * *
  1. private: means private, only this class can access
  2. default: means that there is no modifier and can be accessed in the same class
  3. protected: means that it can be accessed by classes in the same package or by subclasses of other packages
  4. public: all classes can be accessed

Guess you like

Origin blog.csdn.net/qq_45042462/article/details/115278185