Understanding the concept of the package, you can systematically introduce the level of access control in Java. In Java, four access levels are provided for classes, member methods, and properties, respectively private, default, protected, and public.
Permission access modifiers (permissions from large to small right row)
|
Public (Common) |
Protected (Protected) |
Default (Defaults) |
Private (privately) |
The same class |
√ |
√ |
√ |
√ |
Same package |
√ |
√ |
√ |
X |
Child Parent Class |
√ |
√ |
X |
X |
Different packages |
√ |
X |
X |
X |
Public and private here I do not introduce one by one, presumably everyone than I am familiar with it. and protected and default in the same class I do not verify (because the least privileged private can be in the same class to each other access, both of which are sure to do)
Verify that the properties of the protected adornment can be accessed in the same package. :
Package D;class Demo7 {protected int a = 1;}
Package D;public class Demo8 {public static void main (string[] args) {Demo7 z = new Demo7 (); System.out.println ("a=" +z.a);}
Operation Result:
can access to!
verifies that the default (default) decorated property can be accessed in the same package. :
Package D;class Demo7 {int a = 1;}
Package D;public class Demo8 {public static void main (string[] args) {Demo7 z = new Demo7 (); System.out.println ("a=" +z.a);}
Operation Result:
can access to!
Verify that the properties of the protected adornment can be accessed in the child parent class. :
Package D;class Demo7 {protected int a = 1;}
Package D;public class Demo8 extends Demo7 {public static void main (string[] args) {Demo8 z = new Demo8 (); System.out.println ("a=" +z.a);}
Operation Result:
can access to!
verifies that the default (default) decorated property can be accessed in the child parent class. :
Package D;class Demo7 {int a = 1;}
Package E;import d.*;p ublic class Demo8 extends Demo7 {public static void main (string[] args) {Demo8 z = new Demo8 (); System.out.println ("a=" +z.a);}
operation Result:
cannot access the to!
Verify that the properties of the protected adornment can be accessed in different packages. :
Package D;class Demo7 {protected int a = 1;}
Package E;import d.*;p ublic class demo8{public static void Main (string[] args) {Demo7 z = new Demo7 (); System.out.println ("a=" +z.a);}
Operation Result:
cannot access the to!
verifies that the default (default) decorated property can be accessed in different packages. :
Package D;class Demo7 {int a = 1;}
Package E;import d.*;p ublic class demo8{public static void Main (string[] args) {Demo7 z = new Demo7 (); System.out.println ("a=" +z.a);}
Operation Result:
cannot access the to!
ac Penguin: 654249738, and self-taught Exchange group: 517284938
Java_se basic--59. Access modifiers