Access to classes, methods, interfaces, abstract classes in JAVA

access permission  

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

  1. default: that is, without any access modifier, usually called "default access mode". In this mode, access is only allowed within the same package.
  2. protect: An access modifier between public and private, generally called "protected form". The classes, properties 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.
  3. public: The most restrictive modifier in the Java language, generally called "public". The classes, properties, and methods modified by it can not only be accessed across classes, but also across packages.
permission modifier kind variable/method
public Classes inside and outside the package can access Classes inside and outside the package can access
private A class cannot be declared private Only this type of access is allowed
protected A class cannot be declared protected Classes and subclasses within this package can access
Do not write Classes in this package can access Classes in this package can access


(2) access rights to members or methods in the class,

There are four types: public, protected, default, private
scopes Current class Same package descendant class Other packages
public √ √ √ √
protected √ √ √ ×
default √ √ × ×
private √ × × ×

(3) Member or method access rights in abstract classes and interfaces 
    Regarding the access rights of members in an abstract class, it basically inherits the characteristics of the class, but because the abstract class is an abstract class, it is used as a parent class and waits for the subclass to implement it, while the private class in the class is an abstract class. Permissions can only be accessed by themselves, so when the method in the abstract class is abstract, there are only three access permissions: public, protected, and default. The properties of all members in the interface are public static final, which means that the variables declared in the interface are constants and cannot be inherited     . The access properties of all methods in the interface are public, so the methods in the implementation interface must be marked as public, otherwise Compilation error. In a method implementing an interface, throw an exception <= before overriding
   

Partly reproduced in: https://www.cnblogs.com/wxlovewx/p/5189950.htm

Or reproduced in: https://blog.csdn.net/xuqiaobo/article/details/52279840

Or reproduced in: https://blog.csdn.net/cq_jone/article/details/4689392


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325773302&siteId=291194637
Recommended