Document directory
- Accesscontrol. accesscontroldemo
- Accesscontrol. Base
- Test. accesscontroldemo
- Test. Bulk
1. Resolution
Java has four types of access permissions, three of which have access permission modifiers: private, public, and protected, and one without any modifiers.
- PRIVATE: the shortest modifier for access permission restriction in Java, which is generally called "private ". The classes, attributes, and Methods Modified by the class can only be accessed by the objects of the class. The subclass of the class cannot be accessed, and cross-package access is not allowed.
- Default: no access modifier is added, which is usually called "default access mode". In this mode, only access in the same package is allowed.
- Protect: an access modifier between public and private. It is generally called a "protected shape ". Classes, attributes, and Methods Modified by the class can only be accessed by methods and subclasses of the class, even if the subclass can be accessed in different packages.
- Public: the widest modifier in the Java language. It is generally called "public ". Classes, attributes, and Methods Modified by the package can be accessed not only across classes, but also across packages.
The following table shows the similarities and differences between the four access permissions. The table is as follows:
| |
Same class |
Same package |
Subclass of different packages |
Non-subclass of different packages |
|
Private |
√ |
|
|
|
|
Default |
√ |
√ |
|
|
|
Protected |
√ |
√ |
√ |
|
|
Public |
√ |
√ |
√ |
√ |
2. Protected
Assume that there are accesscontroldemo and base classes under the accesscontrol package. Protected double price is a member variable of the base class, because the two classes are in the same package, therefore, you can directly access system in the accesscontroldemo class. out. println (base. price); the specific instance is as follows:
Accesscontrol. accesscontroldemoview code
package accesscontrol;public class AccessControlDemo { public static void main(String[] args) { // TODO Auto-generated method stub Base base=new Base("123-1",120.1); System.out.println(base.price); }}Accesscontrol. baseview code
Package accesscontrol; public class base {private string ISBN; protected double price; // default constructor public base () {}// constructor, if you only define constructors with parameters, instead of Default constructors, the base subclass must define an explicit constructor // implicit super constructor base () is undefined for default constructor. must define an explicit public base (string ISBN, double price) {This. ISBN = ISBN; this. 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 ;}}
However, if we move the accesscontroldemo class to the test package, we will find an error in eclipse and the compilation fails because the member variables of the protected type are invisible in the test package.
Suppose we create a base class subclass bulk in the test package, that is, bulk is a base class.Subclass of different packages. In the bulk class, you can directly access the protected double price. This base-level self-base class member variable is shown in the following example:
Test. accesscontroldemoview code
package test;public class AccessControlDemo { public static void main(String[] args) { Bulk bulk=new Bulk("123-1",120.1); bulk.print(); }}Test. bulkview code
package test;import accesscontrol.Base;public class Bulk extends Base { public Bulk() { super(); } public Bulk(String isbn, double price) { super(isbn, price); } public void print() { System.out.println(this.price); }}