Java modifier: public,protected,private, without modifiers. What difference does it have?

Source: Internet
Author: User
Tags modifiers

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?

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.