Java access modifier and java Modifier
| Modifier |
Description |
| Public |
Common and visible to all classes. |
| Protected |
Protected and visible to the classes and all subclasses in the same package. |
| Private |
Private, visible in the same category. |
| Default |
Visible in the same package. By default, no modifiers are used. |
Public: public
Classes, methods, constructor methods, and interfaces declared as public can be accessed by any other class. if several public classes are distributed in different packages, You need to import the packages where the corresponding public classes are located.
Because of the inheritance of the class, all the common methods and variables of the class can be inherited by its subclass set.
The main () method of the java program must be set to public. Otherwise, the Java interpreter cannot run this class.
Protected: protected
Variables, methods, and constructor declared as protected can be accessed by any other class in the same package, or by sub-classes in different packages.
The protected access modifier cannot modify classes and interfaces. Methods and member variables can declare protected, but the member variables and member methods of the interface cannot be declared as protected.
Subclass can access the methods and variables declared by the protected modifier, so as to protect irrelevant classes from using these methods and variables.
Private
Methods declared as private, variables, and constructor can only be accessed by their classes, and classes and interfaces cannot be declared as private.
Variables declared as private access can only be accessed by external classes through the public getter and setter methods in the class.
The use of the private access modifier is mainly used to hide the implementation details of the class and protect the class data.
- Public class Dog {
- Private String name;
- Private int age;
- Public String getName (){
- Return name;
- }
- Public void setName (String name ){
- This. name = name;
- }
- Public int getAge (){
- Return age;
- }
- Public void setAge (int age ){
- This. age = age;
- }
- }
Default: No keywords are used.
The attributes and Methods declared without any modifier are visible to classes in the same package. The variables in the interface are implicitly declared as public static final.
By default, the access permission for methods in the interface is public.
Access Control and inheritance:
Note the Rules inherited by the following methods:
1. The method declared as public in the parent class, and must be declared as public in the subclass.
2. The method declared as protected in the parent class is declared as either protected or public in the subclass, and cannot be declared as private.
3. The default modifier declaration method in the parent class, which can be declared as private in the subclass.
4. The method declared as private in the parent class cannot be inherited.
How to use the access controller:
1. When the class you want to write is accessed by all 'others' classes, use the public statement.
2. When you want your class to be accessed by the class in your package, you can omit the access control character.
3. When you need to control the member data in a class, you can set the Member Data Access Controller in this class to public, protected, or omitted.