Access modifiers in Java

Source: Internet
Author: User
Tags modifiers

Java has four access rights, three of which have access modifiers, private,public and protected, and one without any modifiers. Where the package represents the default access rights, the code is the default case where no modifiers are written. For the sake of good, name the package for the moment.

  Public protected Private Package  Access control for top-level code resourcesfor Class,interface,enum of file level (top-level), there are 2 access modifiers: Public and the default package. Public : Can be used in any other place. Package: A class,interface or enum that can be used only in the same packet. Other packages cannot be used. Java source code file stipulates that: A. java file can have only one class, interface, or enum that is decorated as public, and the name of the file must be the same as the public modifier. The general habit is a public-modified class, interface, and the enum corresponds to a separate 1. java file.

Access modifiers for class membersPublic : Members can be used by any other code access.

protected: Direct access in this class, other code of the same package can be accessed directly, and any subclass can also be accessed directly. Package: For other code in the same bundle, you can directly visit , equivalent to public, not the same package of other code, then the equivalent of private, hidden. Private: Use can only be accessed in this class.
permission modifier private package< /th> protected public
same class √&NBSP;  √ √&NBSP;
&NBSP; √   √
&nb SP; &NBSP; √&NBSP;
other &NBSP; &NBSP; &NBSP;

Access modifiers in Java

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.