There are four types of access modifiers in Java, private, default, protected, public, and their access rights are raised sequentially, and private can only be called by this class. The default, which defaults to not write, can be called by all classes under the same package, and protected can be called by all classes under the same package, or by subclasses under different packages (that is, there must be an inheritance relationship), and public is open. All classes for all packages under the project can be called.
The code tests are as follows:
,
The test results are as follows:
Except for the private class error, the rest of the access modifier words can be called in other classes of this package.
Testing in different packages:
Neither private nor default can be called in classes that are not in this package;
Protected must be a subclass of the original class, that is, an inheritance relationship, in order to be called in a class other than this package;
Public all packages can be called in the class (No inheritance relationship is required).
Hopefully you can deepen your impressions and understand the knowledge of access modifiers.
Access permission modifiers in Java