Access permission character:
(1) public:
For members: they can be accessed by any other class, whether in the same package or in another package.
For classes: the same is true.
(2)friendly:
To the old members: If a member of the class does not have any permissions modification, then its door is the default package access permissions.
It is not a keyword in Java, but here is a way to express it in person. Other classes in the same package can be accessed, but outside the package
That's not possible. For classs not under the same folder, without Package, Java will automatically see these Classes as under the directory that belongs to this directory
Default Package can call Friendly members in class. For example, the following two classes are in the two files of the same folder, respectively
Although it is not introduced in Package, it belongs to the same Default Package.
class Sundae{ //The following two methods are friendly Sundae(){} Void f() {System.out.println("Sundae.f()"); } public class IceCream{ public static void main (String [string [string ] ARGS) {Sundae x = New Sundae (); xf ();}}
For classes: the classes in the same package can be used. In short, the class can only declare as Public or Friendly.
(3) Private:
For members: only access to the class that the member belongs.
class Sundae{ private Sundae(){}//It can only be called in Sundae class Sundae(int i) {} static Sundae makASundae() { return new Sundae(); } } public cla SS Icecream {Public Static Void Main (String [] args){ // The constructor Sundae() in the Sundae class is private, // So it cannot be initialized //Sundae x = new Sundae(); Sundae y = new Sundae(1);//Sundae(int ) It is Friendly, you can call the sunae z = Sundae.makasundae ();}}
For classes: Class cannot be declared as Private.
(4) Protected:
For members: the classes in the same package can access (package access permissions); the base class gives the access permissions of the members in the base class through the ProteCTED to give the derived classes not all classes (derived class access permissions).
(5) Default (default permissions)
Class, data members, constructive methods, and method members can use the default permissions, that is, not to write any keywords. The default permissions are the same package permissions, and the elements of the same package permissions can only be called in defined their classes and the class of the same package.
Example: Package C05.Local;
Import Pack1.cookie; // Note: Here ChocolateChip inherits the Cookie -like cookie, the bite () method is also in // chocolatechip, you can directly call it with x.bite, but not, because the class chocolateChip // and cookie The class is not in a package, and each has the package access permissions. In order to be able to use x.bite (), the access permissions of // the cookie method must be replaced with the Public or Protect. , this does not meet the privacy requirements, so it is best to set it to protected. It can be accessed smoothly, and it can also be avoided from outside class calls and protect the function of privacy public class ChocolateChip extends Cookie { public ChocolateChip() { System.out .println ("ChocolatEChip Constructor");} Public Static Void Main (String [] ARGS) {chocolateChip x = new chocolateChip (); // can't accer ss bite}} ///: ~
Package Pack1; Public Class Cookie {Public Cookie () {System.out.println ("Cookie Constructor"); ("Bite");}}
There is a better explanation for the permissions of the class:
Access permissions of class class:
Public: It can be used for all class access.
Default: The default can be called Friendly, but there is no Friendly's modifier in the Java language. This should be derived from C ++. The default access permission is package-level access permission.
That is, if a class is written without a write access modifier, then it is the default access permission, and all classes under the same package can be accessed, even if the class can be instantiated (of course, if this class does not have the ability to instantiate, For example, this class does not provide a public constructor).
illustrate:
1. Each compilation unit (class file) can only have one public class
2. The name of the public class (including upper case) must be the same as its class file.
3. A class file (*.java) may not exist.
This form of existence scenario: if we write a class in a package, it is just to cooperate with other classes in the package, and we don't want to write an account to the client (not necessarily a customer with a realistic meaning, It may be a headache to call this class) and it may be a bit of a headache to see it, and it may be possible to completely change the original practice after a period of time, and completely abandon the old version and replace it with a brand new version.
4. Class cannot be private or protected.
5. If you don't want any object that generates a certain class, you can set all the constructor to private. But even so, the object of this class can be generated, which is the member (properties and methods) of the class static can be done.
Comprehensive example:
First.java:
package Number; import Test.*; public class Frist extends Test { protected String s1 = "Hello"; public static void main( String[] args) { String s2 = "java "; //System.out.println (s1 );; Frist T = NEW Frist (); System.out.println (TS); T.Show (); Return;} TEST.java: PABLIC Class Test Protected String s = "hello test"; //It can be accessed by classes and subclasses in the same package. The subclass can be different from the package Test public void show() { Test1 t1 = new Test1(); return; } } class test1 {test1 () {test t = new test (); system.out.println (ts);}}
Output:
java hello test hello test