Detailed explanation of the road to Java (V) access control

In Java, everything has some form of access control.

The control levels of access rights from the largest to the smallest are: public, protected, package access rights (no keywords) and private.

Public, protected and private, these Java access modifiers, are placed before the definition of each member (domain or method) in the class.

1、 Access rights of class members

The only way to gain access to a member is:

  1). Make the member public. Whoever is there can access the member;

  2). By adding modifiers without access permission and placing other classes in the same package, the member is given package access permission, and other classes in the package can access the member;

  3). Inherited classes can access both public and protected members.

  4). Provides accessor and mutator methods to read and change values.

1. Package access

The default access permission does not have any keywords, but the pass refers to the package access permission, which means that all other classes in the current report have access to that member, but for all classes outside the package, this member is indeed private.

Package access combines all related classes within a package so that they can easily interact with each other.

Note: if the two classes are in the same directory and do not set any package name for themselves, Java will automatically regard such files as the default package belonging to the directory, so these files have package access to each other.

The following example illustrates this problem:

2. Public: interface access permission

Using the keyword public means that the subsequent member declaration is available to everyone, especially the client programmers who use the class library.

3. Private: you can't access it

The keyword private indicates that no other class can access the member except the class containing the member. Other classes in the same package cannot access the private members of this class, so it is equivalent to isolating itself.

This function of the private keyword has many uses. For example, it controls how to create objects and prevents others from directly accessing a specific constructor (or all constructors). Look

The following example:

In this example, we can create a Sunday object by calling the makeasundae () method, but not by the constructor.

The same applies to the private domain in the class.

However, it should be noted that because the reference of an object in the class is private, it should not be considered that other objects cannot have the public reference of the object.

4. Protected: inherit access rights

If you create a new package and inherit classes from another package, the only accessible member is the public member of the source package.

Sometimes, the creator of a base class wants to give access to a specific member to derived classes instead of all classes, which needs to be implemented with the keyword protected.

Note that protected also provides package access, that is, other classes in the same package can also access the protected elements of this class.

2、 Interface and Implementation

Access control is often referred to as implementation specific hiding.

Wrapping data and methods into classes and hiding concrete implementations are often collectively referred to as encapsulation.

For two important reasons, access control delimits the boundary of permission within the data type:

1. Set the boundary between what the client programmer can and can't use. You can build your own internal mechanism in the structure without worrying that client programmers will accidentally regard the internal mechanism as part of the interface they use.

2. Separate the interface from the specific implementation.

3、 Access rights for class

In Java, access modifiers can also be used to determine which classes in a library are available to users of the library.

Modifier must be placed before keyword class. For example:

or

You should know that a class cannot be private (if it is private, no other class can access it except this class), or protected (in fact, an internal class can be private or protected, but this is a special case, which will be described in the following articles). It can only be package access permission or public.

If you don't want others to access this class, you can specify all constructors of this class as private to prevent anyone from creating objects of this class. However, there are exceptions. This practice does not prevent you from creating the class inside the static member of the class. Let's look at the following example:

We can see that the constructors of soup1 and Soup2 classes are private. No one can directly use constructors to create objects of this class. But we can also use these two classes: create a static method in soup1, and use the constructor to create a soup1 object and return its reference; The singleton pattern in the design pattern is used to create Soup2. Only one object can be created. The object of Soup2 class is created as a static private member of Soup2, so there is one and only one, and it cannot be accessed unless it is through the public method access().

In addition, some limitations are noteworthy:

1. Each compilation unit can only have one public class.

2. The name of the public class must exactly match the file name given to the compilation unit, including case.

3. If there is no class with public in the compilation unit, you can name the file at will.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>