Understanding Java access Rights Control _java

Source: Internet
Author: User

Let's take a look at the access control in the Java language today. Before discussing access control, let's discuss why you need access control. Consider two scenarios:

  Scenario 1: Engineer A writes a class ClassA, but engineer a does not want ClassA to be accessed by the other classes in the application, so how do you handle it?

  Scenario 2: If engineer a writes a class ClassA, two of which are fun1, FUN2, engineers just want to make fun1 visible, that is, if other engineers call ClassA, only method fun1 can be invoked.

At this point, access control can play a role.

In Java, four types of access control are provided: Default access (Package access), Public,private, and protected.

  Note that all four of these access rights, only the default access and public can be used to modify the class. Modify the variables and methods of the class four permissions are OK. (The classes mentioned in this premises are for external classes, excluding internal classes)

These four types of access control are described below for the members of the cosmetic class and the cosmetic class, respectively.

1. Cosmetic class

  Default access rights (Package access): used to decorate a class, which means that the class is visible only to other classes in the same package.

  Public : used to modify a class to indicate that the class is visible to all other classes.

Here are a few examples to see the difference between the two:

Example 1:

Main.java:

Package com.cxh.test1;
 
 
public class Main {
 
  /**
   * @param args */public
  static void Main (string[] args) {
    //TODO Auto-generat Ed 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)
 
  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 you can see from the code, the decorated people class uses the default access rights, and because the people class and main class are in the same package, the people class is visible to the main class.

Program Run Result:

  

Example 2:

People.java

Package com.cxh.test2;
 
class People {      //Default access permission (Package access)
 
  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 is a hint error in the main class:

  

Tip the Peolple class is not seen in the main class. As you can see from here, if you decorate a class with default access, the class is visible only to other classes in the same package, not to the classes in the different packages.

The people class is visible to the main class, as shown in the quick fix tip of the figure above, to change the default access permission for the people class to public.

2. Methods and variables for modifying classes

  Default access rights (Package access rights): If a class's method or variable is decorated with package access, it means that the method or variable of that class can be called only in other classes in the same package, and the method or variable of the class cannot be called in a class in a different package.

  Private: If the method or variable of a class is decorated with private, then the method or variable of the class can only be accessed in the class itself, not in the class and in other classes.

  protected: If a class's method or variable is protected modified, the class's method or variable can be accessed for the same package class. For classes of different packages, only the classes that inherit from the class can access the methods or variables of the class.

  Public : a method or variable that is decorated by the public and is visible anywhere.

Let's take a few more examples to see the difference between the methods and variables of the scope class:

Example 3:

Main.java no change.

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) return
    name;
   
  void SetName (String name) {  //Default access permission (Package access permission)
    this.name = name;
  }

At this point in the main class it is possible to display call methods GetName and SetName.

But if the people class and main class are not in the same package:

Package com.cxh.test2;  With the main class in a different package public
 
class people {   
 
  private String name = NULL;
   
  Public people (String name) {
    this.name = name;
  }
   
  String GetName () {  ///Default access permission (Package access) return
    name;
   
  void SetName (String name) {  //Default access permission (Package access permission)
    this.name = name;
  }

An error is prompted in the main class at this time:

  

As you can see, if you decorate a class's methods or variables with the default access permissions, you can only access them in other classes in 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, you can display the calling method 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;
  }
}

The error is in main:

  

If a class man inherits people in Com.cxh.test1, the calling method GetName and SetName can be displayed in the Class man:

Package com.cxh.test1;
 
Import Com.cxh.test2.People;
 
public class Mans extends people{public
 
  Mans (String name) {
    super (name);
  }
   
  Public String toString () {return
    getName ();
  }
}

Here are some additional knowledge about Java packages and class files:

1 The package in Java is mainly 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 a public class, and at this point the name of the source code file must be identical to the name of the public class, in addition, 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 named arbitrarily.

The above is the entire content of this article, I hope to help you learn.

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.