There are four permissions in Java, Private,protected,default,public, not write as default, each with the following table:
|
Private
|
Default
|
Protected
|
Public
|
This class can be accessed
|
Is
|
Is
|
Is
|
Is
|
This package is not accessible in this class
|
Whether
|
Is
|
Is
|
Is
|
Subclasses of non-packages can access
|
Whether
|
Whether
|
Is
|
Is
|
Classes not in this package
|
Whether
|
Whether
|
Whether
|
Is
|
The permissions of the class members must not be greater than the permissions of the class, if greater than the permissions of the class prevail. One of the following classes:
Class test{public int i=0; public void Fun () {System.out.println (i); }}
The class member I and fun although the defined permission is public, but because the class's permissions are default, I,fun's permissions are still default.
When accessing a class member through an object, you should also follow the permissions as follows:
Class test{ public static void main (String[] args) { myclass a=new myclass ();    A.FUN1 ();//error, because FUN1 's permission is private a.fun2 (); system.out.println (A.I1);//error because I1 permissions are private system.out.println (A.I2); }}class myclass{ private int i1=0; int i2=0;  PRIVATE VOID FUN1 () { System.out.println ("fun1 ()"); } void fun2 () { system.out.println ("Fun2 ( ) "); }}
This article is from the "11114468" blog, please be sure to keep this source http://11124468.blog.51cto.com/11114468/1735676
Rights Management in Java