每当我们编写我们的类时,我们必须向 JVM 提供一些关于我们的类的信息,比如这个类是否可以从任何地方访问,是否可以创建子类,是否可以创建对象等。我们可以通过在Java使用适当的关键字访问修饰符来指定此信息。因此访问修饰符用于设置类、方法和其他成员的可访问性。
访问修饰符:
- 私人的
- 受保护
- 最终的
在了解它们之间的差异之前,让我们深入讨论它们以更好地理解它们。
私有访问修饰符:此修饰符不适用于顶级类或接口。它仅适用于类中的构造函数、方法和字段。如果变量或方法或构造函数被声明为私有,因为我们只能从类内部访问它们,即从类外部我们无法访问它们。
Java
// Java Program to illustrate Private Access Modifier
// Importing required packages
import java.io.*;
import java.util.*;
// Class 1
// Helper class
class A {
// Method of this class
private void m1() {
// Print statement whenever this method is called
System.out.println("GFG");
}
}
// Class 2
// Main class
class B {
// Main driver method
public static void main(String[] args) {
// Creating an object of above class
A a = new A();
// Accessing the method m1() of above class
// by creating object of above class in
// main() method of this class
a.m1();
}
}
Java
// Java program to illustrate Protected Access Modifier
// import required packages
import java.io.*;
import java.util.*;
// declaring a parent class A
class A {
// declaring a protected method m1()
protected void m1() { System.out.println("GFG"); }
}
// creating a child class by extending the class A
class B extends A {
// main method
public static void main(String[] args)
{
// creating an object of parent class
// using parent reference
A a = new A();
/// calling methid m1
a.m1();
// creating an object of child class
// using child reference
B b = new B();
// calling method m1
b.m1();
// creating an object of child class
// using parent reference
A a1 = new B();
// calling m1 method
a1.m1();
}
}
Java
// Java program to illustrate Final keyword
// import required packages
import java.io.*;
import java.util.*;
// Declaring parent class P
class P {
// Declaring a first name
// method
public void firstName()
{
// Display firstname
System.out.println("Rahul ");
}
/// Declaring a final surName
// method
public final void surName()
{
// Display surname
System.out.println("Trivedi");
}
}
// Creating a child class
// of above parent class
class C extends P {
// overriding the surName
// method
public void surName()
{
// Display surname
System.out.println("Sharma");
}
// Main method
public static void main(String[] args)
{
// Display message
System.out.println("GFG");
}
}
受保护的访问修饰符:此修饰符可应用于数据成员、方法和构造函数,但此修饰符不能应用于顶级类和接口。成员被声明为受保护的,因为我们只能在当前包中访问该成员,但只能在外部包的子类中访问该成员。
Java
// Java program to illustrate Protected Access Modifier
// import required packages
import java.io.*;
import java.util.*;
// declaring a parent class A
class A {
// declaring a protected method m1()
protected void m1() { System.out.println("GFG"); }
}
// creating a child class by extending the class A
class B extends A {
// main method
public static void main(String[] args)
{
// creating an object of parent class
// using parent reference
A a = new A();
/// calling methid m1
a.m1();
// creating an object of child class
// using child reference
B b = new B();
// calling method m1
b.m1();
// creating an object of child class
// using parent reference
A a1 = new B();
// calling m1 method
a1.m1();
}
}
GFG
GFG
GFG
输出说明:在上面的例子中,我们使用父引用和子引用创建了三个对象,并在其上调用了 m1() 方法,并且它成功执行,所以从上面的例子我们可以说我们可以访问当前包内的受保护方法通过使用父引用或子引用在任何地方。
最终访问修饰符:它是适用于类、方法和变量的修饰符。如果我们将父类方法声明为 final,那么我们不能在子类中覆盖该方法,因为它的实现是 final 的,如果一个类被声明为 final,我们就不能扩展该类的功能,即我们不能创建该类的子类,即最终类不可能继承。 final 类中存在的每个方法始终是 final y 默认值,但 final 类中存在的每个变量都不需要是 final。 final 关键字的主要优点是我们可以实现安全性,并且可以提供独特的实现。但是 final 关键字的主要缺点是我们缺少像继承(因为 final 类)、多态性(因为 final 方法)等 OOP 的主要优点,因此如果没有特定要求,则不建议使用 final关键词。
Java
// Java program to illustrate Final keyword
// import required packages
import java.io.*;
import java.util.*;
// Declaring parent class P
class P {
// Declaring a first name
// method
public void firstName()
{
// Display firstname
System.out.println("Rahul ");
}
/// Declaring a final surName
// method
public final void surName()
{
// Display surname
System.out.println("Trivedi");
}
}
// Creating a child class
// of above parent class
class C extends P {
// overriding the surName
// method
public void surName()
{
// Display surname
System.out.println("Sharma");
}
// Main method
public static void main(String[] args)
{
// Display message
System.out.println("GFG");
}
}
输出:
现在,在了解了所有这些之后,让我们来看看它们之间的差异以得出结论。
Private Access Modifier | Protected Access Modifier | Final Access Modifier |
---|---|---|
This modifier is not applicable to top-level classes. | This modifier is not applicable to top-level classes. | This modifier is applicable to top-level classes. |
This modifier is applicable to both the enum and constructor. | This modifier is applicable to both the enum and constructor. | The final modifier is not applicable to both the enum and constructor. |
This modifier is applicable to interfaces. | This modifier is applicable to interfaces. | This modifier is not applicable to interfaces |
This modifier is not applicable to local variables. | This modifier is not applicable to local variables. | This modifier is the only modifier that is applicable to local variables. |
We cannot access private members outside the class. | We can access the protected members outside the class. | We can access the final members outside the class. |
We cannot access private members from the outside package. | We can access protected members from the outside package through child reference. | We cannot access final members from the outside package. |