Access Permissions in Java

Source: Internet
Author: User

private,public,protected, default not written (firendly)

1. Class access rights:
Public: can be accessed by all classes.
Default: The default can be called friendly however, the Java language does not have friendly this modifier, so the salutation should be derived from C + +. The default access rights are package-level access rights.
That is, if you write a class that does not have a write access modifier, it is the default access permission, and the class under the same package can be accessed even if you can instantiate the class
(except, of course, if the class does not have the ability to instantiate, for example, the class does not provide a constructor for public).

Description
1, each compilation unit (class file) can only have a public class
2. The name of the public class (including case) must have the same names as its class file.
3, a class file (*.java) may not exist in the public class.
This form of presence scenario: if we compose a class within a package, just to work with other classes within the same package, and
We don't want to be bothered by the fact that we're writing the documentation to the customer (not necessarily a real-time customer, maybe a class called this class), and it's possible that after a while
It is possible to radically change the original practice and discard the old version completely, replacing it with a new version.
4, class can not be private and protected.
5. If you do not want any object that produces a class, you can set all constructors of that class to private. But even this can generate objects of that class, which is the static members (properties and methods) of class.

2. Access permissions for class member variables:
Public: Properties that are immediately public can be accessed by any class. You can use ClassName.PropertyName directly. However, considering the encapsulation of a class, it is seldom used to define the properties of a class as public.
This is defined when static constants are defined. such as: public static final int page_size=10;
Private: Only methods within the class itself can access the private properties of the class, and of course internal classes can also access the private members of their external classes. (Properties and methods)
Default (Friendly): package-level visibility, classes within the same package can access this property, can be accessed directly using ClassName.PropertyName, but the class's properties are rarely used in this way from the encapsulation nature of the class.
Protected: The keyword deals with the concept of "inheritance". For other classes of the same package, protected= default, for classes of different packages, if there is an inheritance relationship, and baseclass exists protected attribute, it can be self-inheriting, and other classes of different packages
The protected property of the class cannot be accessed.

3. Access rights for member methods of the class:
From a syntactic standpoint, these access-Rights controls act on the same way as the class and on the properties of the class.
Public: All classes are visible.
Pirvate: Only the internal methods of the same class are visible, and the inner classes can be accessed as well.
Default (Friendly): Inside the package is visible.
Protected: Inheritance is visible.

4. Reasons for the existence of access rights
1, "Let the user can not touch the things they should not touch." "These things are used only by the internal mechanisms of the class. Declaring certain members as private is a service to the user.
Because this allows users to know which of his concerns is unimportant and does not require attention, this has historically reduced the difficulty of using the object's knowledge.
2. (final reason) allows programmers to change the way the class works internally without worrying about spilling over to the client program.

5. Java's "access modifier" gives classes developers a very valuable control capability. As a classes developer, if any of your changes can be completely non-intrusive to your users,
You can change your bottom-level implementation with peace of mind because the client programmer cannot access this part of class.
Through today's learning access to the problem learned that the interface or abstraction and so on the strategy so-called "implementation hidden" its root cause is not that in order to hide the implementation of the Code and ideas, but to reduce the user's use of difficulty, and from a maintenance perspective, the visibility of the scope of control to program maintenance brought great security.

Access Permissions 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.