Home > Java > javaTutorial > body text

How to use access modifiers in Java

WBOY
Release: 2023-05-10 19:55:04
forward
975 people have browsed it

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

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 {
    // ...
}
Copy after login

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!");
    }
}
Copy after login

protected

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 {
    // ...
}
Copy after login

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!");
    }
}
Copy after login

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

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 {
    // ...
}
Copy after login

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!");
    }
}
Copy after login

The name and sayHello methods of this class can only be accessed by other classes in the mypackage package.

private

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!");
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!