Controlling Access in Java

Source: Internet
Author: User
Tags modifier

Referrence:oracle Java Doc

Levels

Top Level:public, or package-private (no explicit modifier)

Member Level:public, private, Protected, package-private (no explicit modifier)

Three Modifiers & four Access Control Types

1. Public

A Class/member may being declared public with the modifier, in which case, the class was visible to all classes everywhere .

2. Package-private (no explicit modifier)

If a class/member has no modifier (the default, also known as package-private), it's visible only within its OW N Package.

3. Private

The private modifier specifies that the member can is only being accessed in its own class.

4. Protected

The protected modifier specifies that the member can is only accessed within it own package (as with Package-pri Vate) and, in addition, by a subclass of their class in the another package.

Access levels Chart

1st Col:whether The class itself have access to the member defined by the access level.

2nd Col:whether classes in the same package as the class (regardless of their parentage) has access to the member.

3rd Col:whether Subclasses of the class declared outside This package has access to the member.

4th Col:whether All classes has access to the member.

Controlling Access 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.