Access modifiers in Java are used to limit the access scope of classes, interfaces, fields and methods. They respectively represent different access control levels. There are four access modifiers in Java: public, protected, default and private.
public is the most open access modifier and is used to specify the public access level. Classes, interfaces, fields and methods modified by public can be accessed from anywhere.
For example, the following code defines a public class:
public class MyClass { // ... }
This class can be accessed from anywhere. Additionally, if a method or field is declared public, it can be accessed from anywhere. For example:
public class MyClass { public String name; public void sayHello() { System.out.println("Hello, world!"); } }
protected specifies the protected access level. Classes, fields and methods modified with protected can be accessed by other classes in this package, as well as by subclasses.
For example, the following code defines a protected class:
package mypackage; protected class MyProtectedClass { // ... }
This class can only be accessed by other classes in the mypackage package, and by subclasses that inherit MyProtectedClass. In addition, if a method or field is declared protected, it can also be accessed by other classes in the same package, as well as by subclasses that inherit this class. For example:
package mypackage; public class MyClass { protected String name; protected void sayHello() { System.out.println("Hello, world!"); } }
The name and sayHello methods of this class can be accessed by other classes in the mypackage package, as well as by subclasses that inherit MyClass.
default is the default access modifier in Java, it is also called package-level private access control. If a class, interface, field, or method does not use any access modifiers, then it is the default access level. The default access level means access is only available within the same package.
For example, the following code defines a class with a default access level:
package mypackage; class MyDefaultClass { // ... }
This class can only be accessed by other classes in the mypackage package. In addition, if a method or field does not use any access modifier, then it is also the default access level. For example:
package mypackage; public class MyClass { String name; void sayHello() { System.out.println("Hello, world!"); } }
The name and sayHello methods of this class can only be accessed by other classes in the mypackage package.
private is the most restrictive access modifier and is used to specify a private access level. Fields and methods modified by private can only be accessed within the class in which they are defined.
For example, the following code defines a private class:
public class MyClass { private String name; private void sayHello() { System.out.println("Hello, world!"); } }
The name and sayHello methods of this class can only be accessed inside the MyClass class.
The above is the detailed content of How to use access modifiers in Java. For more information, please follow other related articles on the PHP Chinese website!