Java Access Permissions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.