Java creates an instance of the protected inner class of the outer class by inheriting the outer class

Source: Internet
Author: User
Tags export class

Original link: http://blog.sina.com.cn/s/blog_7de00ff60102xffx.html

If you want to establish an instance of the outer class in an export Class (subclass) of an external class as an inner class of protected permission, you need to set the constructor of the inner class of the protected permission to public. Otherwise, the compiler will give an error.

We might take it for granted that, since I inherited the external class, I have the authority to build an instance of the inner class of the member of this external class--protected permission, which is actually not the case. Since we do not explicitly build the constructor of the inner class of the protected permission, the compiler will automatically build a default constructor for the protected inner class at compile time, and the access rights of the default constructor will be consistent with that of the inner class, protected, This means that only the exported class (subclass) of the inner class and the class that is similar to the inside of a package have permission to invoke the constructor of the protected permission. To create an instance of a class, it must pass through the constructor, since only the exported class (subclass) of the inner class and its constructor that is similar to the inner package class can invoke it, then it is explained by default (the constructor of the inner class that does not own the public permission). Only the exported class (subclass) of the inner class and the class with which the inner package is similar can establish an instance of it.

In summary, if we want to establish an instance of an inner class that is a member of the outer class by inheriting an external class, set the permissions of the constructor of the inner class of the protected permission to public--protected.

This problem arises in the 10th chapter of Programming thinking 6th.

Java creates an instance of the protected inner class of the outer class by inheriting the outer class (GO)

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.