Access modifier: used to modify the classes, attributes, and methods that are accessed
Access modifier | Scope | Can be modified |
---|---|---|
public | any position | Class, attribute, method |
protected | The inherited relationship is accessible | Properties and methods |
Default (no modification) | In the same package | Class, attribute, method |
private | Class | Properties and methods |
bean.User class:
package com.cao.test.word1.bean;
public class User {
public String name = "cao";
public void show() {
System.out.println("show()...");
}
}
Test class:
package com.cao.test.word1;
import com.cao.test.word1.bean.User;
public class Test {
public static void main(String[] args) {
User user = new User();
System.out.println(user.name);
user.show();
}
}
Normal execution
cao
show()...
If the public of the bean.User class is removed , that is:
class User {
public String name = "cao";
public void show() {
System.out.println("show()...");
}
}
At this time, Test will report an error because the default modification + different packages
In the same way, remove the public of member variables and member methods , namely:
public class User {
String name = "cao";
void show() {
System.out.println("show()...");
}
}
Test will also report an error , because the default modification + different packages
If you create a new project, the structure is as follows:
Father class: member variables are modified with protected
public class Father {
protected String name="cao";
}
The Son class inherits the Father class:
public class Son extends Father {
public void show(){
System.out.println(name);
}
}
Test:
public class Test {
public static void main(String[] args) {
Son son = new Son();
son.show();
}
}
carried out:
cao
Because member variables are modified with protected + Son class inherits Father class
Create a new class of Test under the bean package:
bean.Test class:
public class Test {
public static void main(String[] args) {
Father father=new Father();
System.out.println(father.name);
}
}
Remove the protected member variables in the Father class , namely:
public class Father {
String name="cao";
}
Modifications to the default
at this time Son category error , but bean.Test not being given , because bean.Test the same package with the Father.
Change the protected of the member variable in the Father class to private , namely:
public class Father {
private String name="cao";
}
At this time, the Son class reports an error , but bean.Test also reports an error , because the private modification makes the member variables only accessible in the class.
Change the protected of the member variables in the Father class to public , namely:
public class Father {
public String name="cao";
}
At this point, even if Son is not a subclass of Father, you can still access it normally:
Son class:
public class Son {
public void show() {
Father father = new Father();
System.out.println(father.name);
}
}
Execute through the main function of the Test class:
cao
When the access modifier is overridden in the method, the access scope of the parent class is less than or equal to the access scope of the child class
Father class:
public abstract class Father {
abstract void show();
}
Son type:
public class Son extends Father{
@Override
void show() {
System.out.println("show()...");
}
}
Test class:
public class Test {
public static void main(String[] args) {
Son son=new Son();
son.show();
}
}
show()...
If the show() method of the Son class is changed to public modification:
public class Son extends Father{
@Override
public void show() {
System.out.println("show()...");
}
}
Operation is normal and
changed to protected:
public class Son extends Father{
@Override
protected void show() {
System.out.println("show()...");
}
}
Operation is normal
If it is changed to private:
public class Son extends Father{
@Override
private void show() {
System.out.println("show()...");
}
}
The error