JAVA Basic Access control permissions

Source: Internet
Author: User

Packages (Package)

The role of the package

When writing a Java source file, this file is often referred to as Compilation Unit。 Each compilation unit must have a suffix name . Java
In the compilation unit, you can have a PublicClass the name of the class must be the same as the name of the fileOther classes are allowed in the file, but not the public class.
When compiling a . Javafile, each class in the file will have a suffix of . ClassOutput file with the same file name and class name.
A Java runnable program is a set of. class files that can be packaged and compressed into a Java document file (jar, a jar document generator using Java). The Java interpreter is responsible for locating, loading, and interpreting these files. A class library is actually a set of class files. Each of these files has a public classAnd any number of non-public classes
So each file has a component (each with its own separate. java and. class files). If you want these artifacts to belong to the same group, you can use a keyword Package
If you use the package statement, you must be the first line of code in the file except for comments. As follows: PackageAccess; If you want to use an access package, you need to use the keyword import. As follows:Importaccess.*;

Points

To avoid the unique package name, the general naming method is:

(1) The first part should be the anti-order Internet domain name of the creator of the class.

(2) The second part is to decompose the package name into a file directory on the machine.

When the Java program runs and loads the. class file, it can find the appropriate path based on the package name.

eg

PackageNet.mindview.simple;



Java access rights

Access control permissions in Java are based on permissions from large to small: Public> protected> Package access permissions (no keywords)> Private

Note: in order to facilitate the description of the access rights, may wish to make an image metaphor. a class in the same package can be considered a friend relationship ; A class that has an inheritance relationship can be considered a parent-child relationship ; all members of the same class are treated as their own .

Scope Yourself (current Class) Friends (same package) Descendants (Inheritance relationship) Other Package
Public Access to Access to Access to Access to
Protected Access to Access to Access to Not accessible
Friendly Access to Access to Not accessible Not accessible
Private Access to Not accessible Not accessible Not accessible

Package access rights

JAVA 's default access rights do not have any keywords, usually referred to as package access rights (sometimes also expressed as friendly).

This means that all other classes in the current package have access to the members of the package access permission. But for classes that are not in this package, this member is private.

Because a compilation unit (that is, a. java file) can be subordinate to only one package, all classes in the same compilation unit are accessible to each other.

You and your friend class can access it.

Public access rights

Accessible to all .


Private access Rights

It is accessible only to itself (except for the class that contains the member, which is inaccessible to any other class).

Protected access rights

They can be accessed by themselves, by friends , and by descendant classes .


References "Thinking in Java"

JAVA Basic Access control permissions

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.