Java study note 10 --- How to control the access scope of member variables, member methods, and classes using access permission modifiers; study note 10 ---

Source: Internet
Author: User

Java study note 10 --- How to control the access scope of member variables, member methods, and classes using access permission modifiers; study note 10 ---

1. Java has three access permission modifiers: public, protected, and private, and the default permission modifier, which is recorded as default. The class can be modified by public and default. Both of these four modifiers can modify member variables and member methods. Each modifier corresponds to a different access range. The following is an example.

Figure 1

  • Private can only cover circle 1, that is, only this class can be accessed;
  • Default can cover circle 3, that is, this class, the same Steamed Stuffed Bun class, and other classes in the same package can be accessed. Simply put, all classes in the same package of this class can be accessed;
  • Protected can cover circle 4, that is, this class, the same Steamed Stuffed Bun class, other classes in the same package, and subclasses in other packages can be accessed, simply put, classes in the same package as this class and subclasses in other packages can be accessed;
  • Public can cover circle 5, that is, this class, the same Steamed Stuffed Bun class, the same package other classes, other Steamed Stuffed Bun classes, other classes of other packages can be accessed, in short, all classes can be accessed;

Note: In the subclass of a different package from the parent class, if you access and call the variables and Methods Modified by protected in the parent class through the subclass object, you can indeed; however, if you access and call the object through the parent class, you cannot access the variables and Methods Modified by protected. For details, see the following (6) and (7 ). The specific cause is unknown.

 

2. The above conclusions are verified with simple procedures below.

Prerequisites:

  • The human package defines the class Person, Student, and DustMan. Student is a subclass of Person, And DustMan is not a subclass of Person.
  • In the teacher Package, the class Teacher and GateMan are defined. In the package, Teacher is a subclass of Person, And GateMan is not a subclass of Person.
  • Four member variables and four member methods are defined in "Person", which are public, protected, default, and private. For details, see the following code:
        String name;
public String education;private String hobby;protected String residence;public void testModifierPublic() {System.out.println("Public");}protected void testModifierProtected() {System.out.println("Protected");}void testModifierDefault() {System.out.println("Default");}private void testModifierPrivate() {System.out.println("Private");}

  

(1 ),Define the Person Class Object pOwn in the Person class to access and call these member variables and member methods respectively. For details, see the following code:

public static void main(String[] args) {Person pOwn = new Person("xi",20,"female","bachelor","recite","NJ");System.out.println("public education: " + pOwn.education);System.out.println("protected residence: " + pOwn.residence);System.out.println("default name: " + pOwn.name);System.out.println("private hobby: "+ pOwn.hobby);pOwn.testModifierPublic();pOwn.testModifierProtected();pOwn.testModifierDefault();pOwn.testModifierPrivate();}

Output result:

public education: bachelorprotected residence: NJdefault name: xiprivate hobby: recitePublicProtectedDefaultPrivate

Result Analysis: The Person class object can access and call the member variables and member Methods Modified by public, protected, default, and private in this class.

 

(2 ).Define the Student class objects sSamePackChild and Person class objects pSamePackChild respectively in the Student class, and access and call these member variables and member methods respectively. For details, see the following code:

public static void main(String[] args) {Student sSamePackChild = new Student("xi",20,"female","bachelor","recite","NJ");Person pSamePackChild = new Person("xi",20,"female","bachelor","recite","NJ");System.out.println("public education: " + sSamePackChild.education);System.out.println("protected residence: " + sSamePackChild.residence);System.out.println("default name: " + sSamePackChild.name);System.out.println("private hobby: "+ sSamePackChild.hobby);sSamePackChild.testModifierPublic();sSamePackChild.testModifierProtected();sSamePackChild.testModifierDefault();sSamePackChild.testModifierPrivate();System.out.println("public education: " + pSamePackChild.education);System.out.println("protected residence: " + pSamePackChild.residence);System.out.println("default name: " + pSamePackChild.name);System.out.println("private hobby: "+ pSamePackChild.hobby);pSamePackChild.testModifierPublic();pSamePackChild.testModifierProtected();pSamePackChild.testModifierDefault();pSamePackChild.testModifierPrivate();}

Output result:

        Exception in thread "main" java.lang.Error: Unresolved compilation problems: The field Person.hobby is not visibleThe method testModifierPrivate() from the type Person is not visibleThe field Person.hobby is not visibleThe method testModifierPrivate() from the type Person is not visibleat human.Student.main(Student.java:108)    

Result Analysis: A compilation error occurs, indicating that the private modified holobby and testModifierPrivate () are invisible.

 

(3 ).According to the error message (2), comment out the relevant line and execute it again. For details, see the code:

public static void main(String[] args) {Student sSamePackChild = new Student("xi",20,"female","bachelor","recite","NJ");Person pSamePackChild = new Person("xi",20,"female","bachelor","recite","NJ");System.out.println("public education: " + sSamePackChild.education);System.out.println("protected residence: " + sSamePackChild.residence);System.out.println("default name: " + sSamePackChild.name);//System.out.println("private hobby: "+ sSamePackChild.hobby);sSamePackChild.testModifierPublic();sSamePackChild.testModifierProtected();sSamePackChild.testModifierDefault();//sSamePackChild.testModifierPrivate();System.out.println("public education: " + pSamePackChild.education);System.out.println("protected residence: " + pSamePackChild.residence);System.out.println("default name: " + pSamePackChild.name);//System.out.println("private hobby: "+ pSamePackChild.hobby);pSamePackChild.testModifierPublic();pSamePackChild.testModifierProtected();pSamePackChild.testModifierDefault();//pSamePackChild.testModifierPrivate();}

Output result:

public education: bachelorprotected residence: NJdefault name: xiPublicProtectedDefaultpublic education: bachelorprotected residence: NJdefault name: xiPublicProtectedDefault

Result Analysis:

The private modified row is successfully commented out;

The Person class object can access and call the member variables and member Methods Modified by public, protected, and default in the class Student of the same package as the parent class, and cannot access the variables and Methods Modified by private;

The Student class object defined in the subclass also has the same access permission.

 

(4 ).Define the Person Class Object pSamePackNonChild In the DustMan class to access and call these member variables and member methods respectively. For details, see the following code:

package human;public class DustMan {public static void main(String[] args) {Person pSamePackNonChild = new Person("xi",20,"female","bachelor","recite","NJ");System.out.println("public education: " + pSamePackNonChild.education);System.out.println("protected residence: " + pSamePackNonChild.residence);System.out.println("default name: " + pSamePackNonChild.name);System.out.println("private hobby: "+ pSamePackNonChild.hobby);pSamePackNonChild.testModifierPublic();pSamePackNonChild.testModifierProtected();pSamePackNonChild.testModifierDefault();pSamePackNonChild.testModifierPrivate();}}

Output result:

        Exception in thread "main" java.lang.Error: Unresolved compilation problems: The field Person.hobby is not visibleThe method testModifierPrivate() from the type Person is not visibleat human.DustMan.main(DustMan.java:19)

Result Analysis: A compilation error occurs, indicating that the private modified holobby and testModifierPrivate () are invisible.

 

(5 ).According to the error (4), comment out the relevant lines and execute the command again. For details, see the code:

package human;public class DustMan {public static void main(String[] args) {Person pSamePackNonChild = new Person("xi",20,"female","bachelor","recite","NJ");System.out.println("public education: " + pSamePackNonChild.education);System.out.println("protected residence: " + pSamePackNonChild.residence);System.out.println("default name: " + pSamePackNonChild.name);//System.out.println("private hobby: "+ pSamePackNonChild.hobby);pSamePackNonChild.testModifierPublic();pSamePackNonChild.testModifierProtected();pSamePackNonChild.testModifierDefault();//pSamePackNonChild.testModifierPrivate();}}

Output result:

public education: bachelorprotected residence: NJdefault name: xiPublicProtectedDefault

Result Analysis:

The private modified row is successfully commented out;

The Person class object can access and call the member variables and member Methods Modified by public, protected, and default in a non-subclass of the same package as the Person, and cannot access the variables and Methods Modified by private.

 

(6 ).Define Teacher class objects tDiffPackChild and Person class objects pDiffPackChild In the Teacher class, and access and call these member variables and member methods respectively. For details, see the following code:

package teacher;import human.Person;public class Teacher extends human.Person {String duty;public Teacher() {}public Teacher(String d) {super();this.duty = d;}public Teacher(String n, int a, String g, String e, String h, String r) {super(n,a,g,e,h,r);}public static void main(String[] args) {Teacher tDiffPackChild = new Teacher("xi",20,"female","bachelor","recite","NJ");Person pDiffPackChild = new Person("xi",20,"female","bachelor","recite","NJ");System.out.println("public education: " + tDiffPackChild.education);System.out.println("protected residence: " + tDiffPackChild.residence);System.out.println("default name: " + tDiffPackChild.name);System.out.println("private hobby: "+ tDiffPackChild.hobby);tDiffPackChild.testModifierPublic();tDiffPackChild.testModifierProtected();tDiffPackChild.testModifierDefault();tDiffPackChild.testModifierPrivate();System.out.println("public education: " + pDiffPackChild.education);System.out.println("protected residence: " + pDiffPackChild.residence);System.out.println("default name: " + pDiffPackChild.name);System.out.println("private hobby: "+ pDiffPackChild.hobby);pDiffPackChild.testModifierPublic();pDiffPackChild.testModifierProtected();pDiffPackChild.testModifierDefault();pDiffPackChild.testModifierPrivate();}}

Output result:

        Exception in thread "main" java.lang.Error: Unresolved compilation problems: The field Person.name is not visibleThe field Person.hobby is not visibleThe method testModifierDefault() from the type Person is not visibleThe method testModifierPrivate() from the type Person is not visibleThe field Person.residence is not visibleThe field Person.name is not visibleThe field Person.hobby is not visibleThe method testModifierProtected() from the type Person is not visibleThe method testModifierDefault() from the type Person is not visibleThe method testModifierPrivate() from the type Person is not visibleat teacher.Teacher.main(Teacher.java:39)

Result Analysis:

A compilation error occurs. For the defined Teacher Class Object tDiffPackChild, the variable access and method call prompts are as follows: default modified name and testModifierDefault (), private modified holobby and testModifierPrivate () invisible;

For the defined Person Class Object pDiffPackChild, for its variable access and method call prompts, protected modified residence and testModifierProtected (), default modified name and testModifierDefault () private-modified holobby and testModifierPrivate () are not visible.

 

(7 ).According to the error message (6), comment out the relevant line and execute it again. For details, see the code:

package teacher;import human.Person;public class Teacher extends human.Person {String duty;public Teacher() {}public Teacher(String d) {super();this.duty = d;}public Teacher(String n, int a, String g, String e, String h, String r) {super(n,a,g,e,h,r);}public static void main(String[] args) {Teacher tDiffPackChild = new Teacher("xi",20,"female","bachelor","recite","NJ");Person pDiffPackChild = new Person("xi",20,"female","bachelor","recite","NJ");System.out.println("public education: " + tDiffPackChild.education);System.out.println("protected residence: " + tDiffPackChild.residence);//System.out.println("default name: " + tDiffPackChild.name);//System.out.println("private hobby: "+ tDiffPackChild.hobby);tDiffPackChild.testModifierPublic();tDiffPackChild.testModifierProtected();//tDiffPackChild.testModifierDefault();//tDiffPackChild.testModifierPrivate();System.out.println("public education: " + pDiffPackChild.education);//System.out.println("protected residence: " + pDiffPackChild.residence);//System.out.println("default name: " + pDiffPackChild.name);//System.out.println("private hobby: "+ pDiffPackChild.hobby);pDiffPackChild.testModifierPublic();//pDiffPackChild.testModifierProtected();//pDiffPackChild.testModifierDefault();//pDiffPackChild.testModifierPrivate();}}

Output result:

public education: bachelorprotected residence: NJPublicProtectedpublic education: bachelorPublic

Result Analysis:

After the related rows are commented out, the execution is successful;

If the Teacher Class Object is defined in the Teacher Class Object of a different package than the Person class, this object can be used to access and call the member variables and member Methods Modified by public and protected in the Person class, the member variables and member Methods Modified by default and private cannot be accessed or called;

If the Person class object is defined, only the member variables and member Methods Modified by public in Person can be accessed and called through this object, member variables and member Methods Modified by protected, default, and private cannot be accessed or called.

Question: I don't know why there is such a difference.

 

(8 ).Define the Person Class Object pDiffPackNonChild in the GateMan class to access and call these member variables and member methods respectively. For details, see the following code:

package teacher;import human.Person;public final class GateMan {int gateNumber;public GateMan() {}public GateMan(int g) {this.gateNumber = g;}public static void main(String[] args) {Person pDiffPackNonChild = new Person("xi",20,"female","bachelor","recite","NJ");System.out.println("public education: " + pDiffPackNonChild.education);System.out.println("protected residence: " + pDiffPackNonChild.residence);System.out.println("default name: " + pDiffPackNonChild.name);System.out.println("private hobby: "+ pDiffPackNonChild.hobby);pDiffPackNonChild.testModifierPublic();pDiffPackNonChild.testModifierProtected();pDiffPackNonChild.testModifierDefault();pDiffPackNonChild.testModifierPrivate();}}

Output result:

        Exception in thread "main" java.lang.Error: Unresolved compilation problems: The field Person.residence is not visibleThe field Person.name is not visibleThe field Person.hobby is not visibleThe method testModifierProtected() from the type Person is not visibleThe method testModifierDefault() from the type Person is not visibleThe method testModifierPrivate() from the type Person is not visibleat teacher.GateMan.main(GateMan.java:29)

Result Analysis: A compilation error occurs, prompting that the residence and testModifierProtected () modified by protected, the name and testModifierDefault () modified by default, the Hoby and testModifierPrivate () modified by private are invisible.

 

(9 ).According to the error message (8), comment out the relevant line and execute it again. For details, see the code:

package teacher;import human.Person;public final class GateMan {int gateNumber;public GateMan() {}public GateMan(int g) {this.gateNumber = g;}public static void main(String[] args) {Person pDiffPackNonChild = new Person("xi",20,"female","bachelor","recite","NJ");System.out.println("public education: " + pDiffPackNonChild.education);//System.out.println("protected residence: " + pDiffPackNonChild.residence);//System.out.println("default name: " + pDiffPackNonChild.name);//System.out.println("private hobby: "+ pDiffPackNonChild.hobby);pDiffPackNonChild.testModifierPublic();//pDiffPackNonChild.testModifierProtected();//pDiffPackNonChild.testModifierDefault();//pDiffPackNonChild.testModifierPrivate();}}

Output result:

public education: bachelorPublic

Result Analysis:

Comment out the rows modified by protected, default, and private, and the operation is successful;

The Person class object can access and call the member variables and member Methods Modified by the public in non-subclasses of different packages with the Person, and cannot access the variables and Methods Modified by protected, default, and private.

 

(10 ).Define the Person class as the class with the default access permission modification, that is, class Person {,,,,}. Both the teacher Class and GateMan class in the Teacher Package have compilation errors, as shown below:

        Exception in thread "main" java.lang.IllegalAccessError: class teacher.Teacher cannot access its superclass human.Personat java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClass(ClassLoader.java:763)at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)at java.net.URLClassLoader.access$100(URLClassLoader.java:73)at java.net.URLClassLoader$1.run(URLClassLoader.java:368)at java.net.URLClassLoader$1.run(URLClassLoader.java:362)at java.security.AccessController.doPrivileged(Native Method)at java.net.URLClassLoader.findClass(URLClassLoader.java:361)at java.lang.ClassLoader.loadClass(ClassLoader.java:424)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)at java.lang.ClassLoader.loadClass(ClassLoader.java:357)at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)

Result Analysis: the default access permission modifier class can only be accessed by the class in this package, but cannot be accessed by the class in other packages.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.