Java access permission modifier public protected default private usage Summary
1. Access Token:
(1) public:
For members: any other class can access them, whether in the same package or in another package.
For classes: the same is true.
(2) friendly:
For members: If a class member does not have any permission modification, it means the default package access permission, expressed by friendly. Note that friendly is not a keyword in Java, here is my favorite way to use it. Other classes in the same package can be accessed, but not outside the package. For classes in the same folder that do not use a package, Java will automatically call friendly Members in the class as the default package under the directory.
For example, the following two classes are in two files in the same folder, although the package is not introduced, but belong to the same default package.
Class Sundae {
// The default value of the following two methods is friendly.
Sundae ()
{}
Void f ()
{
System. out. println ("Sundae. f ()");
}
Public class IceCream
{
Public static void main (String [] args)
{
Sundae x = new Sundae ();
X. f ();
}
}
For Classes: classes in the same package can be used. In short, the class can only be declared as public or friendly.
(3) private:
For members: they can only be accessed in the class to which the member belongs.
Class Sundae {
Private Sundae () {}// can only be called in Sundae class
Sundae (int I ){}
Static Sundae makASundae (){
Return new Sundae ();
}
}
Public class IceCream {
Public static void main (String [] args ){
// The Sundae class constructor Sundae () is private,
// You cannot use it for initialization.
// Sundae x = new Sundae ();
Sundae y = new Sundae (1); // Sundae (int) is friendly, which can be called here
Sundae z = Sundae. makASundae ();
}
}
For a class: the class cannot be declared as private.
(4) protected:
For members, the class in the same package can be accessed (the package access permission). The base class uses protected to grant the access permission of the members in the base class to not all classes in the derived class (the access permission of the derived class ).
Example: package c05.local;
Import pack1.Cookie;
// Note: Here ChocolateChip inherits the class Cookie, and the bite () method is
// In ChocolateChip, x. bite can be called directly, but not, because ChocolateChip
// The Cookie class and Cookie class are not in the same package and each has the package access permission. To be able to use x. bite (), you must
// Change the access permission of the Cookie method to public or protected, but once changed to public
// You can access the service, which does not meet the privacy requirements. Therefore, it is best to set it to protected to allow smooth access or
// Avoid external class calls and protect privacy
Public class ChocolateChip extends Cookie {
Public ChocolateChip (){
System. out. println ("ChocolateChip constructor ");
}
Public static void main (String [] args ){
ChocolateChip x = new ChocolateChip ();
X. bite (); // Can't access bite
}
}///:~
Package pack1;
Public class Cookie {
Public Cookie ()
{
System. out. println ("Cookie constructor ");
}
Protected void bite () {System. out. println ("bite ");}
}
For a class: the class cannot be declared as protected.
The following provides a better explanation of class permission modification:
Class access permissions:
Public: it can be accessed by all classes.
Default: friendly can be called by default. However, the friendly modifier is not found in java, so the name should come from c ++. The default access permission is package-level access permission.
That is, if you write a class without the write access permission modifier, It is the default access permission, and the classes under the same package can be accessed, even if the class can be instantiated (unless of course this class has the ability to be instantiated, for example, this class does not provide a public constructor ).
Note:
1. Each compilation unit (class file) can have only one public class
2. the name of the public class (including uppercase and lowercase) must be the same as the name of its class file.
3. public class does not exist in a class file (*. java. This form of existence scenario: if we write a class in a package, we only want to work with other classes in the same package, in addition, we do not want to worry about writing instruction documents to customers (not necessarily practical customers, or classes that call this class, in addition, it may take some time to completely change the old version and replace it with a new version.
4. The class cannot be private or protected.
5. If you do not want any object that generates a class, you can set all constructors of the class to private. However, even in this way, the object of this class can be generated, that is, the static member of the class (attributes and methods.