In-depth understanding of access control characters in Java

Source: Internet
Author: User
Tags control characters

Core content:
1. Concepts related to packages in Java
2. Specific usage of access control in Java (detailed)

1. Briefly describe the concept of package in Java

Packages in Java are similar to folders in the operating system, where classes with similar functionality or the same classes are placed in the same package for easy organization and management.
For example, if a class is dedicated to data transfer, put this class in the IO packet.

2. Describe three ways to call a class in a package

1> uses the full name of a class: package name + class name
Note: In the package, the real name of all classes will be the package name + class name combination, so at run time to write the package layer, not directly write the class name.
2> importing all the classes in a package using the import statement
3> importing a specific class in a package using the import statement (common)

3. Briefly describe the precautions for the class front modifier

If a class is not an inner class, then only public or nothing in front of the class can be added PROTECTD or default or private access control, try all classes, including the Startup class.

4. Specific usage of access control in Java

Access controls in Java are public, protected, default (No access control), private, according to the order of access from highest to lowest. The specific use of access controls in Java:

(1) Within a class, all members can access each other, the access control is transparent, invalid, common can call private, private can be called common.

Sample program:

package IT01;publicclass Persion{   public String name;       //public   protected Double score;   //protected   String gender;            //默认访问控制符   privatevoidf()          //private   {      System.out.println(name+"\t"+score+"\t"+gender);    }}
(2) between different classes of the same package, only their private members can not be accessed through the class object name, both members and protected members can access this way. In short: Within the same package, only its private members are not able to access

Sample program:

package IT01;publicclass Persion{   public String name;       //public   protected Double score;   //protected   String gender;            //默认访问控制符   privatevoidf()          //private   {      System.out.println(name+"\t"+score+"\t"+gender);    }}

From the above program it is not difficult to see that only private members F () cannot be accessed.

(3) Among the two classes in which there are inheritance relationships between different packages, only the common members of the common class and the protected members of the common class can be accessed by subclasses in the other package, but outside of a subclass, only members of the common class can be accessed directly.

Sample program:

package IT01;publicclass Persion{   public String name;       //public   protected Double score;   //protected   String gender;            //默认访问控制符   privatevoidf()          //private   {      System.out.println(name+"\t"+score+"\t"+gender);    }}

In the inner of the child class:

As you can see, within a subclass, only members of the parent class and members of the protected type can access it, and the default and private members cannot be accessed
Outside of subclasses:

package it01; Public  class  persion{public  String Name       ;   //public  protected    Double score;   //protected             String gender;   //Default access control  private  void  f  ()//private  {system. Out . println (Name+ "\ t"  +score+ "\ t"  +    gender); }}
package IT02;import IT01.Persion;publicclass Student extends Persion  //Student子类继承父类{}


As can be seen from the above, only members of the parent class can be accessed outside the subclass.

(4) Between two classes in which there is no relationship between packages, only a common member of a common class can be accessed by a class in another package.

Attention:
1> between different packages, only a common class can be used by another package.
2> between different packages, if a class is common, it can only be guaranteed to generate its instance object in another package, but there is no guarantee that members in that class can be called.
Instance Program:

package it01; Public  class  persion{public  String Name       ;   //public  protected    Double score;   //protected             String gender;   //Default access control  private  void  f  ()//private  {system. Out . println (Name+ "\ t"  +score+ "\ t"  +    gender); }}


As can be seen from the program above, only common members of a common class can be accessed in another package between packages.
The specific use of access controls in Java can be described in one of the following tables:

Summarize:
1> within a class, the access control is transparent and invalid.
2> within the same package, only private members cannot access
3> between two classes in which a different package has an inheritance relationship, only public members and protected members can access
4> between different packages, only members can access them
If you have any questions, please leave a message!

In-depth understanding of access control characters in Java

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.