Java 访问修饰符
Java 访问修饰符用于在 Java 中提供访问控制。Java 通过三个关键字提供访问控制 - private、protected和public。我们不需要总是使用这些访问修饰符,因此我们还有另一个修饰符,即“默认访问”、“ package-private ”或“无修饰符”。
Java 访问修饰符
我们可以将 Java 访问修饰符用于类以及类变量和方法。我们只能将“public”或“default”访问修饰符用于 Java 类。
- 如果一个类是“公共的”,那么我们可以从任何地方访问它,即从任何其他包中的任何其他类等。
- 一个源文件中只能有一个“公共”类,文件名应该与公共类名相同。
- 如果该类具有“默认访问权限”,那么只能从同一包中的其他类访问它。
Java 类成员访问修饰符
我们可以为类成员变量和方法使用所有四个访问修饰符。但是,成员访问修饰符规则在类级别访问规则之后应用。例如,如果某个类具有默认访问权限,则它将在其他包中不可见,因此该类的方法和变量也将不可见。我们将分别研究它们,然后我们将使用一个简单的程序展示 Java 访问修饰符的用法。
Java 访问修饰符 - public 关键字
如果类成员是“公共”的,则可以从任何地方访问它。成员变量或方法可以全局访问。这是提供对类成员的访问的最简单方法。但是,我们应该小心将此关键字与类变量一起使用,否则任何人都可以更改其值。通常,类变量被保留为私有的,并提供 getter-setter 方法来处理它们。
Java 访问修饰符 - private 关键字
如果类成员是“私有的”,则只能在同一个类内访问。这是最严格的访问限制,类成员对外部世界不可见。通常,我们将类变量保留为私有,将仅在类内使用的方法保留为私有。
Java 访问修饰符 - protected 关键字
如果类成员是“protected”,那么只有同一包中的类和子类才能访问它。此修饰符对私有访问的限制较少,但对公共访问的限制较多。通常,我们使用此关键字来确保类变量只能由子类访问。
Java 访问修饰符 - 默认访问
If a class member doesn’t have any access modifier specified, then it’s treated with default access. The access rules are similar to classes and the class member with default access will be accessible to the classes in the same package only. This access is more restricted than public and protected but less restricted than private. (Least Accessible) private < default < protected < public (Most Accessible) The below table summarise above access modifiers with respect to different classes in the same package or other packages and subclasses. Let’s write some simple classes where we will see the java access modifiers in action. TestA.java
package com.journaldev.access;
class TestA {
public void methodPublic(){
methodPrivate();
}
protected void methodProtected(){
methodPrivate();
}
void methodDefault(){
methodPrivate();
}
private void methodPrivate(){}
}
Note that TestA class has default access and the private class method is accessible to all other parts of the same class. TestB.java
package com.journaldev.access;
import com.journaldev.access.TestA;
public class TestB {
public static void main(String args[]) {
new TestA().methodPublic();
new TestA().methodProtected();
new TestA().methodDefault();
}
public void methodPublic() {
}
protected void methodProtected() {
}
void methodDefault() {
}
private void methodPrivate() {
}
}
Note that TestB is in the same package as TestA class and hence it is able to access it’s class members. private members are not accessible but all other members are accessible because of the same package. TestC.java
package com.journaldev.access.child;
import com.journaldev.access.TestB;
public class TestC {
public static void main(String[] args) {
new TestB().methodPublic();
}
}
TestB class is accessible because it’s public. Only public members of TestB class is accessible because TestC class is not in the same package nor its subclass of TestB. TestE.java
package com.journaldev.util;
import com.journaldev.access.TestB;
public class TestE extends TestB {
public static void main(String[] args) {
new TestB().methodPublic();
new TestB().methodProtected(); // compile time error
// works, accessing super class protected method using subclass
new TestE().methodProtected();
}
}
Since TestE class is a subclass of TestB, we can access TestB protected members through child class TestE. If we try to access the superclass protected method directly, we will get a compile-time error. That’s all for the java access modifiers, it’s simple to understand. Just don’t confuse with the default and protected access. An easy way to remember is that default access is more restricted than protected and protected members are accessible in subclasses. Recently I made a video to explain java access modifiers in detail, you can watch it below on YouTube. https://www.youtube.com/watch?v=QKjnbC3UBtY