Permission access modifiers in JAVA (public,protected,default,private)

Source: Internet
Author: User
Tags access properties modifiers

There are four access modifiers in Java: public,protected,default,private

Note: This is about access to the properties and methods in the class, not the access rights of the class

1.default: Package access rights

If nothing is written, default defaults. Of course, you can also explicitly give the default permission

The properties and methods of the default adornment, all other classes in the package have access to that member, but no access to all classes outside of the package.

2.public: Interface access rights

Public-decorated properties and methods are visible to each class

3.private: In-Class access rights

Private and public, private-decorated properties and methods are not accessible to other classes except the class itself.

4.protected: Inherit access rights

First, protected provides the package access rights. In other words, if two classes are within the same package, default and protected provide the same access to the class where the caller resides.

If not in a package, protected provides inherited access, which means that subclasses can access properties and methods that are protected decorated in the parent class.

Their relationship is as follows:

In-Class Inherited In-Package All other
Public Is Is Is Is
Protected Is Is Is Whether
Default Is Is Whether Whether
Private Is Whether Whether Whether


Permission access modifiers in JAVA (public,protected,default,private)

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.