Java basics: access modifiers

The so-called access authority refers to the visibility of the member variables, member methods, and inner classes of this class to other classes.

Java has four kinds of access permissions, which are public, protected, default, and private in descending order of permissions. If the access modifier is omitted, the access permission is defualt.

Access control table

Authority In-class Same package Different types of buns Different packages are not sub-categories
private × × ×
default × ×
protected ×
public
  • private: The narrowest modifier in the Java language that restricts access rights, generally referred to as "private". The attributes and methods modified by it can only be accessed by objects of this class, and its subclasses cannot be accessed, let alone cross-package access.
  • default: That is, without any access modifiers, it is usually called "default access permissions" or "package access permissions". In this mode, only access in the same package is allowed.
  • protected: An access modifier between public and private, generally referred to as "protected access rights". The attributes and methods modified by it can only be accessed by the methods and subclasses of the class itself, even if the subclasses are in different packages.
  • public: The modifier with the widest access restriction in the Java language, generally referred to as "public". The modified classes, attributes, and methods can not only be accessed across classes, but also across packages.

    private:

  1. In the current class development, the main method can be used directly with the help of names, and the members can be directly used in the main method of the current class in the way of object management.
  2. Outside the current class, the use of objects (or class names, for static) management calls is restricted. At this time, a classmate asked, can't the private member I developed be used anymore? No, you can use the private member in the public method by developing a public method in the current class, so as to achieve indirect use of the private member.
  3. In inheritance, private members are forbidden to be inherited, which means that in the development of subclasses, the names of private members are refused to be used directly.

    public:

  1. In the current class development, the main method can be used directly with the help of names, and the members can be directly used in the main method of the current class in the way of object management.
  2. Outside of the current class, using the object (or class name, for static) management calls is allowed.
  3. In subclasses, public members are allowed to be inherited, which means that in the development of subclasses, the names of public members can be used directly.

    protected:

    The protected modifier is more cumbersome, and it involves the question of whether it is the same package.

   The case of the same package:

  1. In the current class development, the main method can be used directly with the help of names, and the members can be directly used in the main method of the current class in the way of object management.
  2. Outside of the current class, in the same package, it is allowed to use the object (or class name, for static) to manage calls.
  3. In subclasses, in the same package, protected members are allowed to be inherited, which means that in the development of subclasses, the names of protected members can be used directly. The main method of the subclass can be called by the subclass or the parent class object.
  4. In addition to the current class and subclasses, in the class where the subclass is to be used, in the same package, you can use the subclass or the parent class object to manage the call.

   The situation of different packages:

  1. In addition to the current class, in different packages, using objects (or class names, for static) management calls is prohibited.
  2. In subclasses, in different packages, protected members are allowed to be inherited, which means that in the development of subclasses, the names of protected members can be used directly. The main method of the subclass can only be called by the subclass object. The parent class object is not good, this time is equivalent to the protected member in the parent class cross-package
  3. In addition to the current class and subclasses, in the class where the subclass is to be used, in the same package as the subclass, the protected members in the parent class cannot be called by the subclass object. The parent class object is even worse, this time is equivalent to the protected members in the parent class cross-package. If you want to call the protected member of the parent class through the subclass object, you need to overwrite the member of the parent class again.
  4. In addition to the current class and subclasses, in the class where the subclass is to be used, in the same package as the parent class, the protected members of the parent class can be called through the subclass object management. Parent objects are also available, in the same package.
  5. In the third package, which is different from the parent class and the child class, if you want to call the protected member of the parent class through the child class object, you can override the protected member of the parent class and modify the access limiter to public.

 

Guess you like

Origin blog.csdn.net/PrisonJoker/article/details/105236042