As the saying goes, there is no square or circle without rules. As a rigorous object-oriented high-level programming language, Java naturally has strict control over the entire important issue of permissions.
In Java, you can set access control permissions through some Java keywords;
Mainly include private (private), package (package access permission), protected (subclass access permission), and public (public access permission)
In Java, these statements can modify member variables and methods in a class, but only public and friendly types can modify classes. For example:
Next, let’s explain in detail the differences between these permissions (there is a table at the end of the blog) from low to high according to permissions: (High permissions have all the characteristics of low permissions)
Private:
All private methods and private member variables in a class can only be accessed in the class and not in other classes.
For example:
package Main;public class Private {private int a;private int b;Protected (){this.a=1;this.b=2;//a and b can only be called in Private classes}private void set(){System.out.println(""+a+""+b+"/n");}void Print(){this.set();//Only used here;}}Friendly and protected:
If another class is declared in another class, if these two are in one package, then the other class can access the friendly variables of the other class. Different packages cannot access:
If another class A is declared in another class B, if these two are in one package, then another class B can also access the protected variable (or method) of this other class A. If not in a package, then if class A has a parent class C. If, if there is a protected variable (or method) in the parent class C, as long as B and C are in a package, B can use the protected method inherited from the parent class in A. If A is a subclass of B, if AB is not in a package, then A can use the protected method in B.
For example:
package Main;import Protected.*;public class Main {public static void main(String args[]){Protected jkl=new Protected();jkl.Print();//Calling the protected method of jkl parent class Frindly prot =new Frindly();System.out.println(prot.a);//Calling the friendly variable prot.set() in the Main class;//Calling the friendly method in the Main class}} package Main;public class Frindly {int a,b,c,d;Frindly(){a=b=c=d=1;}void set (){System.out.print(""+a+" "+b+"/n");}}package Main;public class Protectedfather{protected int a;private int b;protected Protectedfather(){this.a=1;this.b=2;//a and b can only be called in Protected}private void set(){System.out.println(""+a+""+b+"/n");}protected void Print(){this.set();//Only used here;}}package Protected;import Main.*;public class Protected extends Protectedfather{public Protected(){super();//Calling the protect constructor method super.a=12 in the parent class in another package;//Calling the protect variable of the parent class}}Public:
If the method or variable in the class is public, it can be called in other classes without restrictions (used in the scoop package). Take A out of Class B in other packages
package Main;import Public.*;public class Main {public static void main(String args[]){int a;Public p=new Public();//Calling the public method in the public class a=pa;//Calling the public variable p.set() in the Public class;}}package Public;public class Public {public int a=12;public void set (){System.out.println("OK/n");}}Summary form:
| Same package | Different packages | |||
Subclass | Non-subclass | Subclass | Non-subclass | ||
Private | yes | no | no | no | no |
Friendly | yes | yes | yes | no | no |
Protected | yes | yes | yes | yes | no |
Public | yes | yes | yes | yes | yes |
Summarize
The above is the entire content of this article about briefly discussing the four types of permissions in Java object-oriented, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!