Default access control for Java members

Source: Internet
Author: User

Default access control for Java members

For a class member variable or member function, if you do not use any modifier in public, protected, or private, the member will be assigned the "default access control" level, that is, package access ).

A package access member can be accessed by other classes in the same package, but cannot be accessed by classes in other packages.

Package access control is weaker than private, but stronger than protected. On the one hand, as long as it is a subclass, no matter whether the subclass and the parent class are in the same package or not, the subclass can access the protected method in the parent class. However, once it is outside the package of the original class, no matter whether it is its subclass or not, it cannot access its members at the package access level. On the other hand, a class can access the package access members of another class in the same package and also its protected members.

(Note: package is a keyword in Java. Although package access is also an access control level, the keyword "package" can only be used to indicate which package the class belongs to, rather than "private ", put it in front of the member variable or function as the access control modifier .)

Access-level protection strength: Public <protected <default <private
 

 

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.