problemJava modifier: public,protected,private, without modifiers. What difference does it have?
Essence Answer
As shown in the following table, y means access (visibility), n means no access, for example, 3rd y of the first row, the variable/method of the class is accessible to the variable/method if it is decorated with public.
Modifier |
Class Internal |
Same bag (package) |
Sub-class |
Other range |
Public |
Y |
Y |
Y |
Y |
Protected |
Y |
Y |
Y |
N |
No modifiers |
Y |
Y |
N or Y (see note) |
N |
Private |
Y |
N |
N |
N |
Description: In particular, "no modifier" is required to specify whether a subclass can access a variable/method without a modifier in the parent class, depending on the location of the subclass. If the child class and the parent class are in the same package, then the subclass can access the non-modifier variable/method in the parent class, otherwise it will not work.
originally thought very simple a question, did not want to record, but saw the answer, only then found oneself formerly wrong. I used to think that no modifier is the same as private, and if you don't add modifiers to the variable, Java defaults to private.
StackOverflow Link: http://stackoverflow.com/questions/215497/ In-java-whats-the-difference-between-public-default-protected-and-private
Column Introduction:
very like StackOverflow, can always find a solution to the problem of incurable diseases. Accidentally found that the site has a list of heat. So select some of the more hot questions, read the answers to each question, and then sort them out according to your own understanding. Therefore, these articles are not true translation, but in accordance with their own understanding of a number of additions and deletions, polishing, hoping to put the above discussion, more streamlined and effective sharing to everyone.
if you want to reprint, please specify the original address
Http://blog.csdn.net/lizeyang
Java modifier: public,protected,private, without modifiers. What difference does it have?