07 packages and Access Permissions

Source: Internet
Author: User


VA package is a class container used to save the namespace of a category.

VBecause the Java compiler generates a bytecode file for each class and the file name is the same as the class name, classes with the same name may conflict. To solve this problem, Java provides a package to manage the namespace.

General Form of package declaration:

Packagepkg; // the first sentence in the Java source code

PKG is the package name. Java uses the file system directory to store packages. The directory name and package name are strictly matched.

VThe Declaration format of a multi-level package:


Packagepkg1.pkg2. pkg3;


VThe package level is also strictly reflected in the file system.

VWe recommend that you use the company domain name to create a better package level;

VThe package name must be in lowercase.

Package Import

VImport package name. (classname | *);


For example, import java. Io. fileinputstream;

We recommend that you use less *;

Untitled package class

VClasses with packages cannot access the default package (untitled package.

§ A famous package cannot call classes in an unknown package;

Access Control

VEncapsulate the code used to connect the data with the data processing code. Encapsulation also provides another important attribute: access control. Access control can prevent object abuse.

VUse the access indicator to restrict the access range.

Access Control

VWhen a class can be accessed, its application scope can be limited by certain access permissions for the member variables and member methods in the class.

In the same class, the same package, different classes, and different packages





VPublic: any other class or object that can see this class can access the data or usage of the variable.

VProtected: the same class, the same package can be used. Classes of different packages must be subclasses of these classes.

VPRIVATE: no other class access or call is allowed.

VFriendly (default, without Modifier on the front side): Only classes that appear in the same package can directly use their data and methods.

What is encapsulation?

VBinds data together with functions to form a new type, which is called encapsulation.

VIn Java, an object is the encapsulation of a group of variables and related methods. The variables indicate the state of the object, and the methods indicate the behavior of the object.

VThrough object encapsulation, the module and Information Hiding are realized. By granting certain access permissions to the class members, the information of the members in the class is hidden.



Key points:

1: package.

A package is a class container used to save the namespace of a category.

Logically, classes are divided.


Resolves conflicts between classes with the same name.

1. classes that can differentiate system names and Systems

2. Implement access permission Control

3. It helps to divide and organize the class order/store/customer in Java applications


2: Package naming rules:

A: The package must be written in the first line of the source file.

B: All package names must be in lower case.

C: (recommended) package uses the company's domain name.


Com. softfz. Project name. Module name


3: packages are logically divided by namespaces.


Physically, it is divided by the file system path.

Com. softfz. One

After compiling the class, put the compiled class in the COM/softfz/one folder.



Java com. softfz. One. Class Name;

Classes in the same layer package structure do not need to be imported into each other

// Solve the conflict between classes with the same name (import a full class name and write one)


4: Functions of the javac-D option

-D: Specifies the location where class files are stored.



How to run classes with package and classpath


C:/> JAVA-classpath F: \ lesson9aa. BB. CC. dd. ee. test_03


Package import: classes under the same package can call each other.


When classes under different packages need to be used under another package, the required class path needs to be imported using the import keyword.


Access to a famous package with no package name:


The classes under the Untitled package can access the classes under the famous package.

Classes under the famous package cannot access the classes under the unknown package.


================ Modifier ========================

1. Modify classes, variables, and methods

2. Flexible application: software programs can simulate real-world systems and improve the reusability, maintainability, scalability, and running performance of software systems.

3. Abstract, static, public, protected, private, and final


============== Access Control modifier ==============================

Applicable: Class, member method, member variable, not applicable to local variables

Role: One of the basic ideas of object-oriented is to encapsulate and implement details and expose interfaces. The Java language uses an access control modifier to control the access permissions of methods and variables of classes and classes, so that only interfaces are exposed to users, but the implementation is hidden.


===================== Access Control ================================

First, you need to understand what access is? There are two types of access:


1. Use "XXXX = new XXX (); X. in the form of YYY; (this form may be called "external access". In addition to being applied to itself and sub-classes, this form can also be accessed by applications in other classes, xxx indicates the defined type. Here, it can be base, sub, subsub, and YYY as methods or attributes );


2. Access is in the form of this. YYY (this form may be called "Internal access", but this access form can only be applied to its own class or subclass ).


PRIVATE: the access permission in the same class. Other classes cannot access private-modified methods and attributes.

Default: All classes under the same package can be accessed. Different packages cannot be accessed using the default class.

Protected: the classes in the same package can be accessed, but different packages cannot be accessed. However, under different packages, the variables and Methods Modified by protected can be accessed by subclass of this class.


Access-level access control modifiers are similar to packages with different sub-classes of the same package

Public √

Protected √-

√ --

Private √ ---


Classc extends classa -- this is a cascading -- can be considered as an object [containing parent class data, naturally accessible to protected attributes]

Classa A = new classa (); --- this is a completely independent object and has nothing to do with Class C. It is in the heap space only in the class C space section.

========== Applicable only to Class and Class Members

Common Operations of eclipseide

1: Create a project (separate the source file from the output file)

2: Create a class and get the package name for the class


Alt +/: Smart sensing

CTRL + Shift + O: import the corresponding package

CTRL + shif + F: Code Formatting Function



5. encapsulation:

Hiding the attributes and implementation details of an object and only making public an interface. Systems communicate with each other through interfaces.


Security and reuse

1. It is easy for users to correctly understand and use the system and prevent modification of system properties. Wire and television.

2. It helps to establish loose coupling between systems and improve system independence. When the implementation of a system changes, as long as its interface remains unchanged, it will not affect other systems.

3. Improve System reusability. Dry Battery: Reusable independent system.

4. Reduce the Risk of constructing a large system. Damaged camera, Dry Battery.



Encapsulation principles

1. Hide as many things as possible and provide a simple interface for external users [handler er] = Method

The higher the degree of system encapsulation, the higher the relative independence, and the more convenient to use.





2. Hide all attributes

1. It is more in line with the rule that the real world uses internal factors.

2. Flexible control over the read and modify access levels of attributes. Electricalpowermeter

3. Prevent Users From mistakenly modifying attributes. Account object setpassword

4. Helps with the Implementation Details of object encapsulation.


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.