Java Accessibility and package explanation

Source: Internet
Author: User

One, access control in Java

1, private modifier, that the members are private, only their own can access;
2, protected, represents the protected permission, embodied in inheritance, that is, subclasses can access the parent class protected members, while other classes within the same package can also access the protected members.
3, no modifier words (default), indicating the package access rights (friendly, the Java language is not friendly this modifier, so that the name should be derived from C + +), the same package can be accessed, access is the package level access rights;
4, public modifier, that the member is open, all other classes can be accessed;

Specifically, the following table is visible:

Table 1-1j access control in AVA

  no TD style= "margin:0px; padding:0px ">
Visibility/Accessibility In the same class In the same package In different packages In the same bun category In different buns category
  public   yes   yes   yes   yes   yes
  protected    yes   yes   no   yes yes
  package    yes   yes   no   yes    no
  private   yes   no   no   no

  Note: Java access control is stuck at the compiler level, that is, it does not leave any traces in the. class file, only access control checks during compilation. In fact, by means of reflection, it is possible to access any member of any class under any package, for example, to access a private member of a class.

Additional notes:
(1) Each compilation unit (class file) can have only one public class
(2) The name of the public class (including case) must be the same as its class file.
(3) The public class may not exist in a class file (*.java).
(4) If we write a class in a package just to work with other classes within the same package, and we don't want to be bothered by the idea of writing a document to the customer (which is not necessarily a real-time customer, perhaps a class that calls this class), or maybe it will change the way it was after a while. , and replaces the old version with a new version, which is typically the default.
(5) class may not be private and protected.

Examples of Use:

Package com.visible.test;/** * Base class */class base{public int a;protected int b;private int c;int D;} /*** Inheriting 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 ();}}

Two packs


1. Package

The package name is generally lowercase, and the first letter of the class name is generally uppercase, so that the package name and the class name can be clearly distinguished when referencing. If the package name is not used before the definition of the class, then the class belongs to the default package.


2. Hello Package
Start with a simple package definition to learn about the use of the package:

Package Testpackage;public class test{public static void Main (String args[]) {  mnode node = new Mnode ();  Node.print ("Hello"); }}

Description
(1) Use the Package keyword to indicate the packages in which the class resides;
(2) The package statement must be at the front of the file;
(3) Javac–d can be used at compile time. Test.java automatic generation of packages required directory;
(4) Java Testpackage.test can be used to execute compiled code;


3, the package of several functions:
(1) Can better organize classes, like folders, folders can be different files in the same folder, and the package can also put different class files in the same package;
(2) To reduce the conflict of class name, which is similar to folders, the same folder files can not be the same name, the files in different files may be duplicate, the same package class names can not be duplicated, the class names in different packages may be repeated;
(3) The class in the package has a certain protective effect, see Java access control;


4, the use of import
(1) Direct reference to the specified class, such as import java.util.Vector;
(2) Refer to multiple classes in a package, such as import java.awt.*. Rather, it does not refer to all classes in java.awt, but only to classes defined as public, and only to classes referenced by the code, so this method of referencing does not degrade the performance of the program;
(3) * Number instead of the class name, can not replace the package name, such as import java.awt.*, only refer to java.awt under the class, and do not refer to the package under java.awt;
(4) Import java.awt.f*, this method of use is wrong;
(5) Import statement before all class definitions, after the package definition;
(6) Import only tells the compiler and the interpreter where to find the definitions of classes, variables, methods, and does not introduce these definitions into the code;


5, the use of classes in the package
The classes in a package can be used in the following ways:
(1) If the class to be used belongs to the Java.lang package, you can refer to the specified class directly using the class name, without adding the package name, because the package Java.lang does not display using import, it is introduced by default;
(2) If the class to be used is in a different package (except Java.lang), then the class can be referenced by the package name plus the class name, such as Java.awt.Font
(3) For classes that are often used (the class is in other packages), it is best to use import to refer to the specified package, such as java.awt.*
(4) If the different packages introduced by import contain the same class names, then the use of these classes must be preceded by the package name
(5) An interface can also belong to a package, or you can use import to introduce classes and interfaces in other packages






Java Accessibility and package explanation

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.