What is Access Control
Access control is the developer (such as writing java underlying implementation of people) to the client programmer (using java man) indicates what is available and what is not available to control access. Access is divided into four levels: public, protected, package access (no keyword / default) and private
Why should there be access control
Initially, when we write the code, more attention is that it can not achieve this function. A long time, in retrospect, we might find a better way to achieve that function, this time we want to refactor code. Reconstruction is rewritten to make the code more readable, maintainable and easier to understand.
But on the one hand we do not want anyone to use this project to have access to everything, on our own code can be modified at will on the other hand we want to modify the code, for others to use this project is no change, because they do not care about how to achieve, only care about how to use. That is, to keep the code used to modify the underlying code when the same method , as different people have different identities of the same power. Ever since java to use access control will change things to remain the same things in separate areas.
How access control
public, protected, package access (no keyword / default) and used to modify private classes, interfaces, methods, variable
(Classes and interfaces can only be public or non-keyword, you can not be a private or protected.)
The figure is four keywords corresponding scope (the current class, package, child-parent) Y represents access, N means no access
1、public
public meaning is public, it can be all the classes access to.
Each coding unit (a Java source code file / a suffix .java files) can have zero or one public class.
2、protected
Also known as inherited protected access
When you want to use this subclass inherits members (not private), but do not want to be too public this member when (not public), use protected.
(But also two cases :
Ⅰ, subclass and parent classes in the same package, then protected, and the other variables can be the same type of access to a package.
Ⅱ, sub-classes and superclasses are not the same package, i.e., when a new packet B inherits from class A packet, the subclass only access to the public.
)
(In addition, protected also provides package access, which means that other classes can access protected elements within the same package)
3、default
default is the default, no write access to the key when the default for this keyword, can be accessed within the same package, would not visit a different package.
4、private
private is the private, keyword modification methods, variables can only be accessed in its class, and other classes to visit, public getter methods can be modified to use this part involving "package" was.
3, no class can be public or keyword can not be a private or protected. If not want other people to access the class, you can all be designated as the constructors private, thereby preventing other people to create the class objects, or create objects (singleton design pattern) inside the static members of the class
class example{
private static example ex=new example();
private example(){}
public static example access(){
return ex;
}
}
//这样的情况下,我们可以进一步改进,记录下创建了多少个对象