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