Java Access Permissions
Java access permission issues java access permission modifier Background:
I was familiar with the four access modifiers, public, default, protected, and private in java, But I encountered the following code:
Package pac1; public class Parent {protected int I; protected class Inner {}} package pac2; import pac1.Parent; public class Child extends Parent {public static void main (String args []) {new Child (). I = 0; Inner I = new Parent (). new Inner ();}}
Problem
The code means that the parent class and the subclass are not in the same package, and the parent class contains the protected internal class. Now we want to generate the internal class object in the parent class in the subclass. However, this error is reported:
Error :( 8, 28) java: Inner () can access protected in pac1.Parent. Inner
Thoughts
The access permission of protected is described in this document. The same package can be accessed at will, and only the like can be accessed under other packages. IndeedInner I = new Parent (). new Inner ();There is no problem on the left, but there is an internal class generation method on the right. Why can't I access it?
Originally, I only considered the package location and inheritance relationship between Parent and Child, but not the internal class and Child are different packages. The Inner's constructor's access permission is not public, therefore, an Inner cannot be generated for a Child with no hierarchy in different packages.
Solution