Access Permissions for Java classes

Source: Internet
Author: User
Tags modifiers

1. Parsing

Java has four access rights, three of which have access modifiers, private,public and protected, and one without any modifiers.

    1. The narrowest modifier for access restrictions in the Private:java language is generally referred to as "private." Classes, properties, and methods that are modified by them can only be accessed by objects of that class, their subclasses cannot be accessed, and cross-package access is not allowed.
    2. Default: That is, without any access modifiers, often referred to as the "Default access mode." In this mode, access is allowed only in the same package.
    3. Protect: An access modifier between public and private, commonly referred to as a "protected form." Classes, properties, and methods that are modified by them can only be accessed by methods and subclasses of the class itself, even if the subclasses are accessible in different packages.
    4. The most restrictive modifier in the Public:java language, commonly referred to as "public". The classes, properties, and methods that are decorated are not only accessible across classes, but also across packages (package).
The following table shows the similarities and differences between the four kinds of access rights, which will be more image. The table looks like this:

The same class

Same package

Sub-classes of different packages

Non-subclasses of different packages

Private

Default

Protected

Public

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 of the adornment class and the decorated class, respectively.

1. Modifier class

Default access (Package access): Used to decorate a class, indicating 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 look at 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-generate D method Stubpeople people = new People ("Tom"); System.out.println (People.getname ());}}

People.java

Package Com.cxh.test1;class People {           //default access rights (packet 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 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

Package Com.cxh.test2;class People {           //default access rights (packet 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 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, not to classes in different packages.

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 the method or variable of that class cannot be displayed in a class in a different package.

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

Package Com.cxh.test1;public class People {       private string name = Null;public people (String name) {this.name = name;} String GetName () {    //Default access rights (package access) return name; void SetName (String name) {   //Default access rights (Package access) 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:

Package com.cxh.test2;    In a different package than the main class, public class people {       private string name = Null;public people (String name) {this.name = name;} String GetName () {    //Default access rights (package access) return name; void SetName (String name) {   //Default access rights (Package access) 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

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:

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:

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.

Access Permissions for Java classes

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.