Java access modifier

Source: Internet
Author: User

Understand the object-oriented thinking of the students, all know that "encapsulation" this basic feature, how to correctly apply the access modifier, just can reflect the good or bad package.

There are four Java access modifiers:

1) Public: Access is the highest, and its decorated classes, class variables, methods all classes are visible.

2) Protected: Its modified classes, class variables, methods are visible in the same package (including homogeneous), and the subclasses of different packages are visible.

3) default modifier, which means that no modifier is added when defining a class, class variable, or method. It is only visible in the same package (including the same).

4) Private: Minimum access, its decorated classes, class variables, and methods are only visible in this class.

Ps:protected and private can only be used in internal classes, and external classes can only be used with public and default.

Give the following table:

For public and private no longer a statement.

Keep in mind the following two points: default focuses on the scope of the package; protected focuses on inheritance.

Protected is relatively special: the same package (containing the same) can access its protected class variables and methods through an instance of the class, except in the same package, only through Super access in the subclass and not through the instance.

Attention to old ginger talk about technology, number: Helojava, or scan the following QR code.


One post per day, technical chicken soup.

Java access modifier

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.