The introduction

Access permissions in Java are easy to understand, but not easy to master completely, especially since the four access permissions are not always available. Let’s take a look at what kind of access you can choose under what circumstances.

I. Introduction to access rights

Access control: Refers to the visibility of this class and its internal members (member variables, member methods, inner classes) to other classes, that is, whether the content is accessible to other classes.

Public > protected > default(package access permission) > private (package access permission)

Access permissions This class This package of classes A subclass Outsourcing classes that are not subclasses
public is is is is
protected is is is no
default is is no no
private is no no no

1. Public: the modified class, variable, and method have access to both internal and external sources; 2, Protected: This permission is designed for inheritance. Protected members are accessible to all subclasses, but only to classes in the same package. Non-subclasses of the package are not. 3. Package Access (default) : Only classes of the same package can be accessed, and all outsourced classes cannot be accessed. 4, private: private permission, only the method of this class can use;

Note: Distinguish between protected and package access rights and use them correctly;

  • Protected is used when a member can be inherited by all subclasses but not accessed by outsourced non-subclasses.
  • Package access is used when a member’s access is only available to classes in the same package, including when the outsourced class cannot inherit the member.

Reasons for using access control:

1) Users should not touch the parts they should not touch; 2) Library designers can change the way classes work internally without worrying about having a significant impact on users;

2. Application scenarios of access control

Access permission usage scenarios can be summarized as the following five scenarios, which have different restrictions on access permission usage:

1. Access control for external classes

An external class (an external interface) is relative to an inner class (also known as a nested class), an inner interface. The access control for an external class can only be public and default.

OuterClass {} public Class OuterClass {} Public class OuterClass {} Public class OuterClass {}Copy the code

2. Access control for members of a class

Class members are divided into three types: member variables, member methods, and member inner classes (internal interfaces)

A member of a class can have four types of access control, that is, all access control rights can be used

public class OuterClass { public int aa; // All classes can access protected Boolean bb; // Void can be used by all subclasses and classes of this packagecc() {//default access to system.out.println ()"Package Access Rights"); } private InnerClass{}} private InnerClass{}}Copy the code

Note: Members of a class refer to global members of the class, not local members (local variables, local inner classes, no local internal interfaces). In other words, local members have no access control, because local members only work in their scope and cannot be accessed by other classes.

  public void count(){// Local member variable public int amount; Public int money; // compile through // partially nested interface class customer{// compile through}}Copy the code

The above two scenarios can be used in almost all cases, but some cases are special and require additional access rights

3. Access permissions for abstract methods

Normal methods can have four access permissions, but abstract methods have one limitation: they cannot be private. That is, abstract methods cannot be private. Otherwise, subclasses cannot inherit and implement abstract methods.

4. Access permissions of interface members

Interface due to its particularity, all members of the access permissions are specified, the following is the interface member access permissions:

  • Variable: public static final
  • Abstract method: public abstract
  • Static method: public static, JDK1.8 only support
  • Internal classes and interfaces: public static

And because everything is mandated by default, when we use it, we don’t have to write all the modifiers, the compiler will do that for us, which is, we can write fewer modifiers, but we can’t write the wrong modifiers.

public interface Interface_Test { public int aa = 6; Static final int bb = 5; // // public static interface cc{}}Copy the code

5. Access to the constructor

The constructor can have any of the above four permissions:

1, use private: generally, it is not allowed to directly construct the object of this class, combined with the factory method (static method), to achieve the singleton mode. Note: None of the subclasses can inherit it. If the class has static members, then the class can be outsourced. If the class has static members, the class can be outsourced. (Perhaps it can be used for the outsourced singleton pattern of this class). Note: An outsourced class cannot inherit from this class; Protected: So that all subclasses can inherit the class, but outsourced non-subclasses can’t access it. 4. Use public: All classes of the wrapper are accessible;

Note: constructor is a bit special, because when the constructor of a subclass is initialized, it calls the constructor of its parent class, so if the constructor of a subclass cannot be accessed, then the constructor of the subclass fails to call, which means that the subclass fails to inherit the parent class!

Reference: http://www.cnblogs.com/jinggod/p/8425423.html

If there are any improper articles, please correct them. You can also pay attention to my wechat public number: Learn Java well and get high quality resources.