An example illustrates the access modifier in Java

  • 2020-04-01 04:09:29
  • OfStack

Access character:
(1) the public:
For members: any other class can access them, whether in the same package or another package.
For classes:       Same thing.
(2) friendly:
For members: if a member of a class does not have any permissions, the door is the default package access

"Friendly" is not a keyword in Java, but a personal preference. Other classes within the same package can be accessed, but outside the package

No. For classes in the same folder that are not packaged, Java automatically identifies those classes as belonging to that directory

Default package, can call the friendly member in the class to each other. The following two classes are in two files in the same folder

, although the package is not introduced, it belongs to the same default package.


   class Sundae{
   //The following two methods default to 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 general, classes can only be declared public or friendly.
(3) private:
For members: access is restricted to the class to which 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 class IceCream{
   public static void main(String[] args){
   //The constructor Sundae() in Sundae class is private,
   //So you can't initialize with it
   //Sundae x = new Sundae();
   Sundae y = new Sundae(1);//Sundae(int) is friendly, which you can call here
   Sundae z = Sundae.makASundae();
   }
   }

For classes: classes cannot be declared private.

(4) protected:
For members: classes in the same package can be accessed (package access rights); Base classes give access to members of base classes through protected derived classes not all classes (derived class access).

(5)default
Classes, data members, constructors, and method members can all use the default permission, that is, do not write any keywords. The default permission is the same package permission, and the same package permission elements can only be called in the class where they are defined, and in the same package class.

Example: package c05.local;


import pack1.Cookie;
//Note: here ChocolateChip inherits the Cookie class and the bite() method
//In ChocolateChip, you can call it directly with x.bite, but you can't because of the class ChocolateChip
//The Cookie class and the Cookie class are not in the same package, and each has access to the package. In order to be able to use x.bite(), you must put
//The access to Cookie methods is changed to public or protected, but once changed to public people
//Can access, this is not up to the privacy requirements, so it is best to set it to protected, can access smoothly, can also
//Avoid outside class calls, protect the role of good 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 classes: classes cannot be declared protected

 

There is a better explanation for the permission modification of a class:

Class access:
Public: can be accessed by all classes.
Default: the default can be called friendly but there is no friendly modifier in the Java language, so the name should come from c++. The default access is package-level access.
                That is, if a class is written without a write access modifier, it is the default access, and classes under the same package can be accessed, even if the class can be instantiated
                Except, of course, if the class does not have the ability to instantiate, such as if the class does not provide a constructor for public.

Description:
            1. Each compilation unit (class file) can have only one public class
            2. The name of the public class (including case) must be the same as its class file.
            3. There can be no public class in a class file (*.java).
              Scenario where this form exists: if we write a class in a package, it is simply to work with other classes in the package, and
            We don't want to have to worry about writing documentation for a client (not necessarily a real-world client, but maybe a class that calls this class), and it may take a while
            It is possible to completely change the practice and replace the old version with a new one.
            4. Class cannot be private or protected.
            5. If you don't want any object that generates a class, you can set all constructors of that class to private. But even that can generate objects of that class, which are static members (properties and methods) of the class.

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); 
System.out.println(s2); 
 
Frist t = new Frist(); 
System.out.println(t.s); 
t.show(); 
return; 
} 
 
} 
 
Test.java: 
package Test; 
 
 
public class Test  
{ 
protected String s = "hello test"; //It can be accessed by classes in the same package and by subclasses that are different from the package Test
public void show() 
{ 
Test1 t1 = new Test1(); 
return; 
} 
 
 
} 
 
class Test1 
{ 
Test1() 
{ 
Test t = new Test(); 
System.out.println(t.s); 
} 
} 

Output:  


java 
hello test 
hello test 


Related articles: