Java has four access rights, three of which have access modifiers, private,public and protected, and one without any modifiers.
- The narrowest modifier for access restrictions in the Private:java language is generally referred to as "private." Classes, properties, and methods that are modified by them can only be accessed by objects of that class, their subclasses cannot be accessed, and cross-package access is not allowed.
- Default: That is, without any access modifiers, often referred to as the "Default access mode." In this mode, access is allowed only in the same package.
- Protect: An access modifier between public and private, commonly referred to as a "protected form." Classes, properties, and methods that are modified by them can only be accessed by methods and subclasses of the class itself, even if the subclasses are accessible in different packages.
- The most restrictive modifier in the Public:java language, commonly referred to as "public". The classes, properties, and methods that are decorated are not only accessible across classes, but also across packages (package).
The following table shows the similarities and differences between the four kinds of access rights, which will be more image. The table looks like this:
|
The same class |
Same package |
Sub-classes of different packages |
Non-subclasses of different packages |
Private |
√ |
|
|
|
Default |
√ |
√ |
|
|
Protected |
√ |
√ |
√ |
|
Public |
√ |
√ |
√ |
√ |
Java Class access rights