Brief analysis of access rights of java Modifier of Power node Java College Collation

  • 2020-06-23 00:20:37
  • OfStack

Java has four types of access rights, three of which have access rights modifiers, private, public and protected, and one without any modifier:

private: The narrowest accessor in the Java language, commonly referred to as "private." Classes, properties, and methods that are decorated can only be accessed by objects of that class, not by subclasses of that class, let alone by cross-package access.

default: that is, without any access modifiers, is often referred to as the "default access mode". In this mode, access is allowed only within the same package.

3. protect: An access modifier between public and private, commonly referred to as a "protective shape". Classes, properties, and methods that are decorated can only be accessed by methods and subclasses of the class itself, even if the subclasses are in different packages.

public: The widest access-restricted modifier in the Java language, generally called "public". The classes, properties, and methods it decorates can be accessed not only across classes, but also across packages (package).

The following table shows the similarities and differences between the four access permissions to make it more visual. Note the difference between protected and default, as shown in the table below:

权限修饰符
同1个类
同1个包
不同包的子类
不同包的非子类
Private
 
 
 
Default
 
 
Protected
 
Public


Related articles: