Java access and package explanation

Source: Internet
Author: User

Java access and package explanation

1. Access Control in Java

1. private modifier, indicating that the member is private and can only be accessed by itself;
2. protected indicates the protected permission, which is embodied in inheritance. That is, the subclass can access the protected members of the parent class, and other classes in the same package can also access the protected member.
3. No modifier (default) indicates the package access permission (friendly, java language does not have the friendly modifier, so it should be called from c ++ ), the same package can be accessed. The access permission is package-level access permission;
4. public modifier, indicating that the Members are public and all other classes can be accessed;

See the following table:

Access Control in Table 1-1 J ava

Visibility/accessibility In the same category In the same package In different packages In the same Steamed Stuffed Bun category In different steamed buns
Public Yes Yes Yes Yes Yes
Protected Yes Yes No Yes Yes
Package Yes Yes No Yes No
Private Yes No No No No

Note: The Access Control of Java stays at the compilation layer, that is, it does not leave any trace in the. class file, and only checks the access control during compilation. In fact, through reflection, it is possible to access members of any class under any package. For example, it is also possible to access private members of the category class.

Note:
(1) Each compilation unit (class file) can have only one public class
(2) The name of the public class (including uppercase and lowercase) must be the same as the name of its class file.
(3) public class does not exist in a class file (*. java.
(4) If we write a class in a package, we only want to work with other classes in the same package, in addition, we do not want to worry about writing instruction documents to customers (not necessarily practical customers, but classes that call this class; or it may take a while to completely change the original practice and replace the old version with a new version. In this case, the default version is usually used.
(5) the class cannot be private or protected.

Example:

Package com. visible. test;/*** basic class */class Base {public int a; protected int B; private int c; int d ;} /*** inheritance class */class Deprive extends Base {public void set () {a = 1; B = 2; // c = 3; // error d = 4 ;}} public class Main {public static void main (String [] args) {Base m_Base = new Base (); m_Base.a = 1; m_Base. B = 2; // m_Base.c = 3; // error m_Base.d = 4; Deprive m_deprive = new Deprive (); m_deprive.set ();}}

Second Package


1. Package

The package name is generally in lower case, and the first letter of the class name is generally in upper case. In this way, the package name and class name can be clearly distinguished during reference. If the package name is not defined before the class definition, the class is the default package.


2. Hello Package
First, you can use a simple package definition to learn how to use a package:

package testPackage;public class Test{ public static void main(String args[]) {  mNode node = new mNode();  node.print("hello"); }}

Note:
(1) Use the package keyword to specify the package where the class is located;
(2) The package statement must be at the beginning of the file;
(3) javac-d. Test. java can be used during compilation to automatically generate the directory required for the package;
(4) You can use java testPackage. Test to execute the compiled code;


3. Several Functions of the package:
(1) Better organize classes. packages are similar to folders. Folders can put different files in the same folder, and packages can also put different class files in the same package;
(2) reduce the issue of class name conflicts, which is similar to folders. Files in the same folder cannot be renamed, files in different files can be renamed, and class names in the same package cannot be duplicated, the class names in different packages can be repeated;
(3) It protects the classes in the package. For details, see java access control;


4. Use of import
(1) directly reference the specified class, such as import java. util. Vector;
(2) Reference multiple classes in a package, such as import java. awt .*. More specifically, it does not reference java. all classes in the awt, and only reference the class defined as public, and only reference the class referenced by the code, so this reference method does not reduce the program performance;
(3) * replaces the class name, and cannot replace the package name, such as import java. awt. *, only references java. classes under awt, without referencing java. packages under awt;
(4) import java. awt. F *. This method is incorrect;
(5) Before defining all classes, the import Statement is after the package definition;
(6) import only tells the compiler and interpreter where class, variable, and method definitions can be found, but does not introduce these definitions into the Code;


5. Use of classes in the package
Classes in the package can be used in the following mechanisms:
(1) If the class to be used belongs to java. lang package, you can directly use the class name to reference the specified class, without adding the package name, because the package java. lang does not need to display the use of import, which is introduced by default;
(2) If the class to be used is in other packages (except java. lang), you can reference this class by adding the class name to the package name, such as java. awt. Font.
(3) For frequently used classes (this class is in other packages), it is best to use import to reference the specified package, such as java. awt .*
(4) If different packages imported by import contain the same class name, the use of these classes must contain the package name.
(5) The interface can also belong to a package, or you can use import to introduce classes and interfaces in other packages.






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.