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 | * | * | * | * |
- private: means private, only this class can access
- default: means that there is no modifier and can be accessed in the same class
- protected: means that it can be accessed by classes in the same package or by subclasses of other packages
- public: all classes can be accessed