Detailed explanation of Public,protected,default and private permissions issues

Source: Internet
Author: User

Detailed explanation of Public,protected,default and private permissions issues

Let people better understand the Public,protected,default and private permissions issues between them, I will make an intuitive table. Through the step-by-step experiment, fill out this form complete, so that after doing a summary, so that their relationship between the four, it will be very intuitive display, easy to understand, clear. as is the detailed explanation, may be longer, if the reader wants to know the conclusion and the result directly, can look directly after five steps complete form, the final conclusion.

The form is as follows:

This way, I will complete this form step by step, the first experiment, examine the current package, the same class, the four of them have a difference.

I. Current package the same class

In the third method, there is no default, that is omitted, and must be omitted, written out is not right. To output the result is:

Four can be very good output, so write a table,

Two. Current Package class

to compile this, the result is:

It can be seen that, in addition to the private, are compiled through, so complete the table:

Three. Other classes in the other bag

we set up two files in separate packages.

File 1

In the other package, create a file 2

Compile the file 1 first, and then we'll try compiling the file 2 to see how the results are:

It is clear that except for the public can compile, the other three can not, so we re-complete the table:

Four. Classes that own the inheritance relationship

So we can build two files on the experiment, one is the parent class, one subclass, and in the same directory:

File 1

File 2

So let's run it and see the results:

It can be seen that only private can not, so continue to refine the table:

Five. Classes that inherit relationships from other packages

This we, like the previous one, set up a parent class, a subclass, but not in the same package:

File 1

File 2

So let's run a little bit,

As can be seen, private and protected are not output, complete the last item of the table,

In this case, the scope of the Public,protected,default and private permissions will come out and summarize

(1) Public is common, no permissions, as long as can be found, it can be called.

(2) Protected if there is an inheritance relationship, it can be called wherever it is. However, if there are no inheritance relationships, they cannot be called when different packages are available.

(3) Default is not an inheritance relationship, as long as it is a different package, it can not be called.

(4) Private can only be used in the same class inside the current package.

So the question of authority between them is clear. Thanks for watching Astro Boy's blog with little meatballs!

Detailed explanation of Public,protected,default and private permissions issues

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.