Chapter 6 - Access Control

think in java study notes Chapter 6 - Access Control

1. What is Access Control

Know JAVA applications by a number of different classes cooperate with each other to achieve a series of function calls, I now have a class A, class B two classes, Class B had a lot of methods and properties, through its method of class B set different keywords and attributes, such class a class B can only be used in a particular keyword modification methods and properties, which is access control

2. How to implement access control

Want to know how to implement access control, to know some of the concepts: a compilation unit and some special keywords such as public private protected.

2.1 What is a compilation unit

Know JAVA code is .java file format like this, after javac compiler can be programmed to .class such a machine can identify the machine code. Such a * .java file is a compilation unit.

2.2 features a compilation unit

  1. A compilation unit at most only a class to be public (can not one), and the public of the class must complete and file the same name, the case must be consistent.
  2. A compilation unit can have an unlimited number of default (without any modifiers) of the class, to provide support for this compilation unit public that class.

Demo.java assume the existence of a file, which is written the code can have these types of scenarios.

  1. The only one public class.
public class Demo{}
  1. Also we have a class without modifiers.
public class Demo{}
class Helper{}
class Utils{}
  1. class only with no modifier (generally less than at this time to the file name not required).
class Helper{}
class Utils{}

3. Impact of classes on different modifiers access to

2.2 lists all the possible add modifiers before the class, either public, either by default nothing, the difference between them lies.

  1. public Modified class, there can access.
  2. Default with what is not, and has become a package access, only in the class in the same package can access under this category.

If what is not band, then if the class com.pikzas located under the package, in other classes at the same class path can be accessed without modifiers that can be new Helper (); and can correctly translated, but if a class located in com.alex or com or com.pikzas.inner to these paths, in these classes new Helper (); this operation prompts a compilation error, because the do not have access.

The effects of different modifiers to access the properties and methods 4

4.0. Access range from small to large are

This class can use their own <classpath same package, different classes may be employed <anywhere in any class may be used

Respectively corresponding keyword is private, default, public

4.1.public modified property or method, as long as access to the class, then this method or property can be accessed

4.2.private modified property or method, this is the only class in order to access their own (even within the same compilation unit in different class can not access)

4.3 default default package access is what keywords are also no modification of the property or method, only

4.4 protected this key appears mainly to solve the access problems caused by inheritance, the parent class protected modified property or method is visible to the subclass for the same class package classpath

Guess you like

Origin www.cnblogs.com/Pikzas/p/11210542.html