How to differentiate public,private,protected in Java

Source: Internet
Author: User

    1. In the Java language we describe a class or a method, often using public , private,protected Three keywords, there is also the default access rights, that is, nothing to write.

       

       

      First we introduce the Public keyword, from the literal point of view, publicly meant to be common, it can be seen that its access is very lenient, is everyone's public.

      Public access is the most lax, it can be accessed within the same package, or it can be accessed within different packages, which means that the element immediately followed is available to anyone,

    2.   the

      is then private, literally, it means private, and it can be seen that the access rights are more stringent.

      It represents an element that cannot be accessed by anyone other than the type creator and the internal method of the type. It's like a wall, and when someone tries to access it, they get an error message at compile time.

    3.  

      Protected, which is literally protected, is equivalent to the private keyword, The difference is that the inherited class can access the protected member, but cannot access the private member.

    4.  

      Finally, Java also has a default access permission that, when not used to any of the previously described access-specific permissions, will work, which is known as package access , because under this permission, a class can access members of other classes in the same package, but outside of the package, these members behave as if they were private.

      Summary: Generally we use public to modify the tool class when it is defined, because it is obvious that the tool class is to be available to everyone, so it must be defined as public. And in other classes that you don't want others to be able to make new, We usually use private. A summary of the various visits is shown in the picture.

How to differentiate public,private,protected 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.