We often use four modifiers to modify variables, methods, and classes, but are the four scopes the same? Private and public may be the most known, but protected and default may not know what scope their specific scopes are. The four types of explanation are then proved by the case:
Public : Has the maximum access rights. can be called by any class under the same project, and is generally used for external situations.
protected: Unlike public, classes under different packages are not available, except for descendant classes. so I think this is specifically designed for the sub-category.
Default: It is designed for this package and it is modified to be accessed by other classes under this package.
private: Provided only for the class itself. is the embodiment of a package.
Case: Declare a class that can be inherited by subclasses.
PackageCom.yuanfy.test.scope1;/*** @Description: Parent class with variables and methods modified by four modifiers *@authorYUANFY * @date December 8, 2017 pm 5:04:35*/ Public classPerson {PrivateString name; intAge ; protectedString color; PublicPerson () {} PublicPerson (String name,intAge , String color) { Super(); This. Name =name; This. Age =Age ; This. color =color; } Public voidsay () {System.out.println ( This. Name + "Say:i am" + This. Color + "Person"); }}
Test 1: Test four scopes under the same package, from which you can see that private does not support the use of other classes under the same package, and the other three are supported under the same package other classes are called.
PackageCom.yuanfy.test.scope1;/*** @Description: Test four scopes under the same package *@authorYUANFY * @date December 8, 2017 pm 5:11:12*/ Public classPersontest { Public Static voidMain (string[] args) { person person=NewPerson ("James", +, "red"); System.out.println ( "Name:" + person.name);//compilation error, so private decoration is not in the same package under the other class accessSystem.out.println ("Age:" + person.age);//defaultSystem.out.println ("color:" + person.color);//protectedPerson.say ();// Public }}
Test 2: Under the descendant class Test four scopes (the case of different packages, the same package equivalent to test 1), from the following test code can be seen:private and default modified, do not support in the descendants of the downgrade, the other two are supported in the descendants of the downward use of the class.
1 Packagecom.yuanfy.test.scope2;2 ImportCom.yuanfy.test.scope1.Person;3 /**4 * @Description: Test four scopes under descendant class5 * @authorYuanfy6 * @date December 8, 2017 pm 5:24:307 */8 Public classBlackpersonextendsperson{9 Ten PublicBlackperson (String name,intAge ) { One Super(Name, age, "black"); A } - Public Static voidMain (string[] args) { -Blackperson person =NewBlackperson ("James", 32); the - System.out.println ("Name:" + person.name),//private compile error, so private decoration is not under the different packages of descendants of the class access to the System.out.println ("AG E: "+ person.age);//default compilation error, so the default modifier is not under the different packages of descendants of the class access -System.out.println ("color:" + person.color);//protected +Person.say ();// Public - } +}
Test 3: Test four scopes under different packages, and from the test case below you can see that only public supports other classes under different packages (except descendant class) calls, and none of the other three are supported.
1 Packagecom.yuanfy.test.scope2;2 3 ImportCom.yuanfy.test.scope1.Person;4 5 /**6 * @Description: Test four scopes under different packages7 * @authorYuanfy8 * @date December 8, 2017 pm 5:11:129 */Ten Public classPersonTest2 { One Public Static voidMain (string[] args) { APerson person =NewPerson ("James", +, "red"); - - System.out.println ("Name:" + person.name),//private compile error, so private modifies any class that cannot be accessed under any other package (except for itself). System.out.printl N ("Age:" + person.age);//default compilation error, so the default modifier is not accessible under other packages. System.out.println ("color:" + person.color),//protected compile error, so the default modifier is not accessible under other packages. -Person.say ();// Public - } +}
So through the above case analysis can be drawn to their respective effective scopes, such as the following table:
Scope |
Current class |
In the same package |
Descendants category (different packages) |
Other Packages |
Public |
√ |
√ |
√ |
√ |
Protected |
√ |
√ |
√ |
X |
Default |
√ |
√ |
X |
X |
Private |
√ |
X |
X |
X |
java keyword (i) modifier scope for private, protected, public, and default