Today, let's take a look at access control in the Java language. Before discussing access control, let's discuss why you need access rights control. Consider two scenarios:
Scenario 1: Engineer A has written a class ClassA, but engineer a doesn't want ClassA to be accessed by other classes in the app, so what's the deal?
Scenario 2: If engineer a writes a class ClassA, there are two methods Fun1, Fun2, the engineer just wants to make fun1 visible to the outside, that is, if another engineer calls ClassA, only the method fun1 can be called, then what should be handled?
At this point, access rights control can be useful.
In Java, four types of access control are available: Default access (Package access), Public,private, and protected.
Note that only the default access permissions and public can be used to decorate classes, with the four access rights mentioned above. Modify The variables and methods of the class four permissions are allowed. (The class described in this site is for an external class, excluding an inner class)
The following are the four types of access control that are described for the members (variables and methods) of the decorated and decorated classes, respectively.
1. Modifier class
Default access (Package access): Used to decorate the class, indicating that the class is only for the same package [sub-package also not!!! For example, Com.example.class1.java is the default access permission, it cannot be visible in the other classes in import imports in Com.example.test.class2.java.
Public: Used to modify a class to indicate that the class is visible to all other classes.
Here are a few examples to look at the difference between the two:
Example 1:
Main.java:
12345678910111213141516 |
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
123456789101112131415161718 |
package
com.cxh.test1;
class
People {
//默认访问权限(包访问权限)
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, the decorated people class takes the default access rights, and because the people class and the main class are in the same package, the people class is visible to the main class.
Program Run Result:
Example 2:
People.java
123456789101112131415161718 |
package
com.cxh.test2;
class
People {
//默认访问权限(包访问权限)
private String name =
null
;
public
People(String name) {
this
.name = name;
}
public String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
}
|
What happens when the people class and the main class are not in the same package?
The following are the errors in the main class for hints:
Tip the Peolple class is not seen in the main class. As you can see from here, if you use default access to decorate a class, the class is visible only to other classes in the same package, and the classes in different packages are invisible [the child package is also not visible].
As shown in the quick fix prompt, changing the default access permission for the people class to public would make the people class visible to the main class.
2. Methods and variables for modifying classes
Default access (Package access): If a method or variable of a class is decorated with a package access permission, it means that a method or variable of that class can be called only in other classes in the same package, and that the method or variable of the class cannot be displayed in a class in a different package [the child package is also not visible].
Private: If a method or variable of a class is modified by private, the method or variable of the class can only be accessed in the class itself, and cannot be accessed outside of the class or in any other class.
Protected: If a method or variable of a class is protected decorated, the class's methods or variables can be accessed for the same package class. for classes of different packages, only classes that inherit from the class can access methods or variables of that class .
Public: Methods or variables that are modified by public are visible everywhere.
Here are a few examples to look at the differences between the methods and variables of their scope classes:
Example 3:
Main.java no change.
People.java
123456789101112131415161718 |
package
com.cxh.test1;
public
class
People {
private
String name =
null
;
public
People(String name) {
this
.name = name;
}
String getName() {
//默认访问权限(包访问权限)
return
name;
}
void
setName(String name) {
//默认访问权限(包访问权限)
this
.name = name;
}
}
|
At this point the main class is able to display the calling methods GetName and SetName.
But if the people class and the main class are not in the same package:
123456789101112131415161718 |
package
com.cxh.test2;
//与Main类处于不同包中
public
class
People {
private
String name =
null
;
public
People(String name) {
this
.name = name;
}
String getName() {
//默认访问权限(包访问权限)
return
name;
}
void
setName(String name) {
//默认访问权限(包访问权限)
this
.name = name;
}
}
|
In the main class, an error is displayed:
It can be seen that if the method or variable of a class is decorated with default access permissions, it can be accessed only in other classes of the same package.
Example 4:
People.java
123456789101112131415161718 |
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;
}
}
|
In this case, the calling method GetName and SetName can be displayed in main.
If the people class and the main class are in different packages:
123456789101112131415161718 |
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;
}
}
|
The error will be in main:
If you set a class man in Com.cxh.test1 to inherit people, you can display the calling method GetName and SetName in the class man:
1234567891011121314 |
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 additional information about Java packages and class files:
1) Packages in Java are primarily designed to prevent class file naming conflicts and facilitate code organization and management;
2) For a Java source code file, there can be only one public class if there is a public class, and the source code file must have exactly the same name as the public class, and if there are other classes, these classes are not visible outside the package. If the source code file does not have a public class, the name of the source code file can be arbitrarily named.
original Haizi Avicii made some modifications and additions.Original source:http://www.cnblogs.com/dolphin0520/ This blog does not indicate that the article reproduced by the author Haizi and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Summary of Java Access control review