Today we will learn about access rights control in Java. Before discussing access control, let’s discuss why access control is needed. Consider two scenarios:
Scenario 1: Engineer A wrote a class ClassA, but Engineer A does not want ClassA to be accessed by other classes used in the application, so how should we deal with it?
Scenario 2: If Engineer A writes a ClassA class, which has two methods fun1 and fun2, the engineer only wants fun1 to be visible to the outside world. That is to say, if other engineers call ClassA, they can only call method fun1, then what should we do?
At this time, access permission control can play a role.
In Java, four types of access rights control are provided: default access rights (packet access rights), public, private, and protected.
Note that only the default access permissions and public can be used to modify the class for the above four types of access permissions. The four permissions of the modified class are OK. (The classes mentioned in this place are targeting external classes, not internal classes)
The following will explain these four access rights controls for members of the modification class and the modification class.
1. Modification category
Default access permissions (package access permissions): If used to modify a class, it means that the class is only visible to other classes in the same package.
public: If used to modify a class, it means that the class is visible to all other classes.
Let’s take a look at the difference between the two through a few examples:
Example 1:
Main.java:
package com.cxh.test1; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub People people = new People("Tom"); System.out.println(people.getName()); } } People.java
package com.cxh.test1; class People { //Default access permission (package access permission) private String name = null; public People(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}As can be seen from the code, modifying the People class uses default access permissions, and since the People class and Main class are in the same package, the People class is visible to the Main class.
Program running results:
Example 2:
People.java
package com.cxh.test2; class People { //Default access permission (package access permission) private String name = null; public People(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}At this time, the People class and the Main class are not in the same package. What will happen?
Here are the errors prompted in the Main class:
It prompts that the Peolple class is not visible in the Main class. From this we can see that if a class is modified with default access permissions, the class is only visible to other classes in the same package and is not visible to classes in different packages.
As shown in the quick correction prompt in the figure above, if you change the default access permissions of the People class to public, the People class will be visible to the Main class.
2. Methods and variables of modifying classes
Default access permissions (package access permissions): If a class's methods or variables are modified by package access permissions, it means that the class's methods or variables can only be called displayed in other classes in the same package, and the class's methods or variables cannot be called displayed in classes in different packages.
private: If a class's methods or variables are modified by private, then the class's methods or variables can only be accessed in the class itself and cannot be displayed in the class or in other classes.
protected: If a class's methods or variables are modified by protected, the methods or variables of this class can be accessed for the same package class. For classes with different packages, only classes inherited from that class can access methods or variables of that class.
public: Methods or variables modified by public are visible anywhere.
Let’s take a few examples to see the differences between their scoped class methods and variables:
Example 3:
Main.java has not changed
People.java
package com.cxh.test1; public class People { private String name = null; public People(String name) { this.name = name; } String getName() { //Default access permission (package access permission) return name; } void setName(String name) { //Default access permission (package access permission) this.name = name; }}At this time, the calling methods getName and setName can be displayed in the Main class.
But if the People class and Main class are not in the same package:
package com.cxh.test2; //It is in a different package from the Main class public class People { private String name = null; public People(String name) { this.name = name; } String getName() { //Default access permission (package access permission) return name; } void setName(String name) { //Default access permission (package access permission) this.name = name; }}At this time, an error will be prompted in the Main class:
From this we can see that if the default access permission is used to modify the class's methods or variables, they can only be accessed in other classes of the same package.
Example 4:
People.java
package com.cxh.test1; public class People { private String name = null; public People(String name) { this.name = name; } protected String getName() { return name; } protected void setName(String name) { this.name = name; }}At this time, you can display the calling methods getName and setName in Main.
If the People class and Main class are in different packages:
package com.cxh.test2; public class People { private String name = null; public People(String name) { this.name = name; } protected String getName() { return name; } protected void setName(String name) { this.name = name; }}An error will be reported in Main:
If a class Man is specified in com.cxh.test1 to inherit People, you can display the calling methods getName and setName in the class Man:
package com.cxh.test1; import com.cxh.test2.People; public class Man extends People{ public Man(String name){ super(name); } public String toString() { return getName(); }}Here are some knowledge about Java packages and class files:
1) The packages in Java are mainly used to prevent class file naming conflicts and facilitate code organization and management;
2) For a Java source code file, if there is a public class, there can only be one public class, and the name of the source code file must be exactly the same as the name of the public class. In addition, if there are other classes, these classes are invisible outside the package. If the source code file does not have a public class, the name of the source code file can be named as it pleases.
The above is all about this article, I hope it will be helpful to everyone's learning.