Java four access control permissions

1. Background:

        Understanding class member access control permissions in Java

2.Java has four access control permissions:

        private,protected,public,default

What are their specific access rights? Let’s analyze it with an example:

There are several scenarios here: internal access, inheritance relationship, same package, different packages ;

(1) First we need to create two packages:

com.test.accessrights.pk1 and com.test.accessrights.pk2 are used to test the differences in access rights between different packages.

Then create a Father class as the parent class, place it in the pk1 package, and then create the following four attributes:

public class Father {
    private String param1 ="这是private";
    protected String param2 ="这是protected";
    public String param3 = "这是public";
    String param4 ="这是default";
}

Then test whether the four properties can be accessed inside the Father class:

public class Father {

private String param1 ="这是private";
protected String param2 ="这是protected";
public String param3 = "这是public";
String param4 ="这是default";

public static void main(String[] args) {
    Father father = new Father();
        System.out.println("father 实例访问:"+father.param1);
        System.out.println("father 实例访问:"+father.param2);
        System.out.println("father 实例访问:"+father.param3);
        System.out.println("father 实例访问:"+father.param4);}
}

There are no compilation errors in the code. It can be seen that all four types support intra-class access .

Next, create a Child class in the pk1 package, inherit the Father class, and access properties through the Father object and the Child object respectively.

public class Child extends Father{

public static void main(String[] args) {

Father father = new Father();
System.out.println(father.param2);
System.out.println(father.param3);
System.out.println(father.param4);

Child child = new Child();
System.out.println(child.param2);
System.out.println(child.param3);
System.out.println(child.param4);

}

}

It can be seen that in the same package, subclasses can access the attributes and methods of types other than private types of the parent class .

(2) So if they are not in the same package , are the access rights of the subclass still the same as the above example?

We create a Child2 class in the pk2 package, inherit from the Father class, create a Father object, and access its properties to find that only param3, which is the public type, can be accessed, and other types cannot be accessed.

 

Then I create an object of Child2 class and access the parent class attributes through the subclass to see how it goes. I find that it can access param2 and param3, which are protected and public types.

 

It can be seen from this that for private , it can only be accessed within the class . For protected , currently, in addition to internal access , it can also be accessed by subclasses , even in different packages. For default , in addition to internal access , subclasses must meet the conditions of the same package if accessed . For public , there is currently no restriction .

(3) So are there any other situations? Let's test what classes in the same package that are not in an inheritance relationship can access.

In the pk1 package, create a Stranger class, create Father, Child, Child2 objects, and see how to access the properties.

public class Stranger {

public static void main(String[] args) {

Father father=new Father();
System.out.println("father对象访问:"+father.param2);
System.out.println("father对象访问:"+father.param3);
System.out.println("father对象访问:"+father.param4);

Child child = new Child();
System.out.println("child对象访问:"+child.param2);
System.out.println("child对象访问:"+child.param3);
System.out.println("child对象访问:"+child.param4);

Child2 child2 =new Child2();
System.out.println("child2对象访问:"+child2.param2);
System.out.println("child2对象访问:"+child2.param3);}
}

Both the Father object and the Child object can access other attributes other than param1, which shows that protected in the same package satisfies non-subclass access in the same package, and default also satisfies non-subclass access in the same package .

Child2 is not a class in the same package for Stranger, so Stranger can only access the param2 and param3 attributes of Child2. What does this mean?

We then create a Stranger2 class in pk2 and use it to access the properties corresponding to Father, Child, and Child2.

public class Stranger2 {

public static void main(String[] args) {

Father father=new Father();
System.out.println("father 实例访问:"+father.param3);
Child child = new Child();

System.out.println("child 实例访问:"+child.param3);

Child2 child2 = new Child2();
System.out.println("child2 实例访问:"+child2.param3);}
}

Finally, I found that only param3, which is public, can be accessed. So far, we can know that at least public has no restrictions, while private has the most restrictions . So what are the rules for the other two, protected and default.

As can be seen from the above example, in the same package, the protected attribute of the Father class can be accessed by subclasses, and can also be accessed by other classes in the same package. In other packages, the protected attribute can only be accessed in subclasses through subclasses. Object access cannot be accessed through the father object, and cannot be accessed in other classes . In the same package, the default attribute of the Father class can be accessed by subclasses and can also be accessed by other classes in the same package . From Stranger2 The conclusion from the examples in the class is that default cannot be accessed by other classes in different packages, but this attribute cannot be accessed through child2 in the Stranger class, and through the example of the Child2 class, it can be seen that the child2 object cannot be accessed either Accessing the default attribute of the Father class means that default cannot be accessed by subclasses of other packages, so default cannot be accessed by classes in other packages, whether it is a subclass or not.

[Use a table to summarize]

Access control permissions (yes means you can access)
private default protected public
same category yes yes yes yes
classes in the same package yes yes yes
Subclass yes yes
Classes in other packages yes

Guess you like

Origin blog.csdn.net/u013302168/article/details/132483164