Access rights modifiers in Java __java

Source: Internet
Author: User
Tags modifiers

There are only three access modifiers public,protected and private in Java.

1, if you do not write any modifiers, the default is "package access permissions." The equivalent of all other classes and methods within the package is public, and the classes and methods outside the package are private. Because the grace of a compilation unit (a Java file) is attached to a package, all classes that are in the same compilation unit are automatically accessible to each other through package access. (This is why a package exists)

2. Inherited classes can access both public members and protected members.

3, access rights to obtain the means:

1. Make the access to this member public.

2. By modifying the word without access and placing other classes in the same package, access is based on other member packages.

3. Inherit a class

4. Provide a Get (), set () method. One, public is available to all members, especially the client programmer who uses the class library, and second, private except for the class to which the private-decorated member is located, no other class can access the member. The default access permission is a common privilege. It's also a few days away. Any permission control is automatically available, so you may not think that you will often need to use the keyword private, because without him to work, however, private permissions are very important, especially in a multithreaded environment. third, protected protected deal with the concept of inheritance. If you create a new package and inherit the class from another package, the only member that can be accessed is the public member in the source package. Protected also provides package access, meaning that other classes in the same package that are not inherited from him can access protected-decorated methods or properties. Four, very important point 1, for class access, only two choices, package access rights and public. Neither can it be private nor can it be protected. (internal classes can have private and protected)

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.