Access permission modifiers in Java

Source: Internet
Author: User
Tags modifiers

There are four types of access modifiers in Java, private, default, protected, public, and their access rights are raised sequentially, and private can only be called by this class. The default, which defaults to not write, can be called by all classes under the same package, and protected can be called by all classes under the same package, or by subclasses under different packages (that is, there must be an inheritance relationship), and public is open. All classes for all packages under the project can be called.

The code tests are as follows:

The test results are as follows:

Except for the private class error, the rest of the access modifier words can be called in other classes of this package.

Testing in different packages:

Neither private nor default can be called in classes that are not in this package;

Protected must be a subclass of the original class, that is, an inheritance relationship, in order to be called in a class other than this package;

Public all packages can be called in the class (No inheritance relationship is required).

Hopefully you can deepen your impressions and understand the knowledge of access modifiers.

Access permission modifiers 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.