Reprint: http://www.cnblogs.com/xwdreamer/archive/2012/04/06/2434483.html
1. Parsing
Java has four access rights, three of which have access modifiers, private,public and protected, and one without any modifiers.
- The narrowest modifier for access restrictions in the Private:java language is generally referred to as "private." Classes, properties, and methods that are modified by them can only be accessed by objects of that class, their subclasses cannot be accessed, and cross-package access is not allowed.
- Default: That is, without any access modifiers, often referred to as the "Default access mode." In this mode, access is allowed only in the same package.
- Protect: An access modifier between public and private, commonly referred to as a "protected form." Classes, properties, and methods that are modified by them can only be accessed by methods and subclasses of the class itself, even if the subclasses are accessible in different packages.
- The most restrictive modifier in the Public:java language, commonly referred to as "public". The classes, properties, and methods that are decorated are not only accessible across classes, but also across packages (package).
The following table shows the similarities and differences between the four kinds of access rights, which will be more image. The table looks like this:
|
The same class |
Same package |
Sub-classes of different packages |
Non-subclasses of different packages |
Private |
√ |
|
|
|
Default |
√ |
√ |
|
|
Protected |
√ |
√ |
√ |
|
Public |
√ |
√ |
√ |
√ |
2.Protected
Suppose there are accesscontroldemo and base two classes under Package AccessControl, where protected double price; is a member variable of the base class because two classes are in the same package, So you can directly access System.out.println (Base.price) in the Accesscontroldemo class, with the following examples:
AccessControl. Accesscontroldemo
Package AccessControl; Class Accesscontroldemo { void// TODO auto-generated method stub base base=new Base ("123-1", 120.1); System.out.println (Base.price); }}
AccessControl. Base
PackageAccessControl;PublicClassBase {PrivateString ISBN;ProtectedDoublePrice//Default constructorPublicBase () {}//constructor, if you define only the constructor with parameters and do not define a default constructor, the subclass of base must define an explicit constructor//Implicit super constructor Base () is undefined for default constructor. Must define an explicitPublic Base (String ISBN,DoublePrice) {THIS.ISBN =ISBNThis.price = Price;} public String Getisbn () { return ISBN;} public void SETISBN ( String ISBN) {THIS.ISBN = ISBN;} public double GetPrice () { return Price;} public void setprice ( Double price) {this.price = Price;} }
But if we move the Accesscontroldemo class into the test package, we will find that the error is prompted in Eclipse and the compilation cannot pass because the member variables of the protected type are not visible in the test package.
Suppose we create a subclass of the base class in the test package bulk, which means that bulk is a subclass of the base class's different packages. Then in the bulk class can directly access the protected double price; This base-level member variable from the base class is as follows:
Test. Accesscontroldemo
Package test; Class Accesscontroldemo { void main (string[] args) {Bulk bulk=new Bulk ("123-1", 120.1); Bulk.print (); }}
Test. Bulk
Access Permissions for Java classes