Why can't the external class of Java be decorated with private, protected

Source: Internet
Author: User
Tags modifiers

For top-level classes (external classes), there are only two modifiers: public and default. Because the previous unit of the outer class is a package, the outer class has only two scopes: the same package, anywhere. Therefore, only two control permissions are required: Package control and public access, and two control modifiers: publicly and default.

If the class uses the private modifier, the description is an inner class. The upper level of the inner class is an external class, so there are four access control modifiers: This class (private), the same package (default), the parent-child class (protected), and any location (public). When a private adornment is used by an inner class, it can only be used inside the outer class of the class.

These are all common use, but why is this happening?

Consider that a Java project cannot be done in a class. In the MVC pattern, the class is divided into three layers, and the class is called from layer to layer. It cannot be called if it is defined as private and protected. In other words, for a Java file, either run alone, or be called by another program as a library, if a Java file class is private, then it is not other programs or classes can not use it, then he as a separate file is useless. If it runs as a single file, the class loads how to find it, because it is not visible externally. At the same time, the existence meaning of the class is lost. Therefore, the class has only public and default modifiers.

Why can't the external class of Java be decorated with private, protected

Related Article

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.