Analysis of access control in Java

Source: Internet
Author: User

Analysis of access control in Java

Today, let's take a look at Access Control in Java. Before discussing access permission control, let's first discuss why access permission control is required. Two scenarios are considered:

Scenario 1: engineer A has compiled A class ClassA, but engineer A does not want the ClassA to be accessed by other classes used in the application. What should I do?

Scenario 2: If engineer A has compiled A class ClassA with two methods fun1 and fun2, the engineer only wants to make fun1 visible to the outside. That is to say, if another engineer calls ClassA, only the method fun1 can be called. How can this problem be solved?

In this case, access control can be used.

Java provides four types of access permission control: default access permission (package access permission), public, private, and protected.

Note that only the default access permission and public permission can be used to modify the class. Modify the class variables and methods. (The classes mentioned in this section are for external classes, excluding internal classes)

The following describes the four access permission Control Methods for the modifier class and modifier class members respectively.

1. modifier class

Default access permission (package access permission): used to modify the class, indicating that the class is only visible to other classes in the same package.

Public: indicates that the class is visible to all other classes.

The differences between the two are as follows:

Example 1:

Main. java:

package com.cxh.test1;public class Main {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubPeople 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 ;}}

From the code, we can see that the modified People class adopts the default access permission. Because the People class and Main class are in the same package, the People class is visible to the Main class.

Program running result:

  

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 ;}}

What happens when the People class and Main class are not in the same package?

The following error is prompted in the Main class:

  

The prompt is that the Peolple class is invisible in the Main class. It can be seen from this that if you modify a class with the default access permission, this class is only visible to other classes in the same package, but not to classes in different packages.

As shown in the quick fix prompt, if the default access permission of the People class is changed to public, the People class will be visible to the Main class.

2. Methods and variables for modifying classes

Default access permission (package access permission): If a class's methods or variables are modified by the package access permission, this means that the methods or variables of this class can only be explicitly called in other classes in the same package. The methods or variables of this class cannot be explicitly called in classes of different packages.

Private: if the 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, access is not displayed outside of the class or other classes.

Protected: if the method or variable of a class is modified by protected, the method or variable of this class can be accessed for the class of the same package. For Classes of different packages, only the classes that inherit the class can access the methods or variables of the class.

Public: The methods or variables modified by public are visible anywhere.

The following examples show the differences between the methods of their scope classes 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 ;}}

In this case, the calling methods getName and setName can be displayed in the Main class.

However, if the People class and Main class are not in the same package:

Package com. cxh. test2; // 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 ;}}

In this case, an error is prompted in the Main class:

  

It can be seen that if the default access permission is used to modify the methods or variables of the class, the access can only be performed 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;}}

In this case, the call Methods getName and setName can be displayed 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 you set a class Man in com. cxh. test1 to inherit People, you can display the call Methods getName and setName in 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();}}

The following provides some knowledge about Java packages and class files:

1) Java packages are mainly used to prevent class file name 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 the same as that of the public class. In addition, if other classes exist, 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 at will.

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.