The difference between private, protected, public, and Default in Java (detailed) _java

Source: Internet
Author: User
Tags control characters modifier modifiers

(1) for the public modifier, it has the largest access and can access any one of the classes, interfaces, exceptions, and so on under Classpath. It is often used for external situations, i.e. the form of an interface to an object or class.

(2) for the protected modifier, its main function is to protect the subclass. It means that subclasses can use it to modify the members, others can not, it is equivalent to pass to the subclass of an inherited thing.

(3) for default, a bit of time also become friendly (friend), it is designed for this package access, any in this package of classes, interfaces, exceptions, etc., can access each other, even if the parent class is not decorated with protected members can also be.

(4) for private, its access is limited to the interior of the class and is an embodiment of encapsulation, for example, most of the member variables are modifiers private and do not want to be accessed by any other external class.

The following table is the meaning and usage of Java access control characters

Class Internal This package Sub Class External Package
Public
Protected X
Default X X
Private X X X

Note: Java access control is stuck at the compile level, which means that it does not leave any traces in the. class file and only checks for access control at compile time. In fact, by means of reflection, it is possible to access members of any class under any package, for example, to access the private members of a class.

Difference:

(1) Public: can be accessed by all other classes.

(2) Private: can only be accessed and modified by themselves.

(3) Protected: itself, subclasses and classes in the same package can be accessed.

(4) Default: Classes in the same package can be accessed, and declarations without modifiers are considered friendly.

The above is a small series for everyone to bring the private in Java, Protected, public and default of the difference (detailed) all content, I hope that we support cloud Habitat Community ~

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.