每当我们编写我们的类时,我们必须向 JVM 提供一些关于我们的类的信息,比如这个类是否可以从任何地方访问,是否可以创建子类,是否可以创建对象等等。我们可以通过在Java称为访问修饰符的适当关键字来指定此信息。因此访问修饰符用于设置类、方法和其他成员的可访问性。
修饰符 1:公共访问修饰符
如果一个类被声明为公共类,那么我们可以从任何地方访问该类。
在下面的示例中,我们在该包中创建了一个包 pack1,我们声明了一个公开的类 A,在该类中,我们声明了一个也是公开的方法 m1。现在我们创建另一个包 pack2 并在该包中导入 pack1 并声明一个类 B,在类 B 的 main 方法中,我们创建一个类 A 类型的对象并尝试访问方法 m1 的数据。
例子:
Java
// Java program to showcase the example
// of public access modifier
// creating a package
package pack1;
// import required packages
import java.io.*;
import java.util.*;
// declaring a public class
public class A {
// declaring method m1
public void m1() { System.out.println("GFG"); }
}
Java
// creating a package
package pack2;
// import required packages
import java.io.*;
import java.util.*;
// importing package pack1
import pack1.A;
// driver class
class B {
// main method
public static void main(String[] args)
{
// creating an object of type class A
A a = new A();
// accessing the method m1()
a.m1();
}
}
Java
// Java program to showcase the example
// of 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 showcase the example
// of private access modifier
// import required packages
import java.io.*;
import java.util.*;
// helper class
class A {
// helper method
private void m1() { System.out.println("GFG"); }
}
// driver class
class B {
// main method
public static void main(String[] args)
{
// creating an object of type class A
A a = new A();
// accessing the method m1()
a.m1();
}
}
Java
// Java Program to illustrate Package Level Access Modifier
// Importing utility classes
// Importing input output classes
import java.io.*;
import java.util.*;
// Main Class
class GFG {
// Declaring default variables that is
// having no access modifier
String s = "Geeksfor";
String s1 = "Geeks";
// Method 1
// To declare a default method
String fullName()
{
// Concatenation of strings
return s + s1;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating an object of main class(GFG)
// in the main() method
GFG g = new GFG();
// Calling method1 using class instance
// and printing the concation of strings
System.out.println(g.fullName());
}
}
使用以下命令行编译并保存上述代码:
Java
// creating a package
package pack2;
// import required packages
import java.io.*;
import java.util.*;
// importing package pack1
import pack1.A;
// driver class
class B {
// main method
public static void main(String[] args)
{
// creating an object of type class A
A a = new A();
// accessing the method m1()
a.m1();
}
}
如果在编译 B 类时 A 类不是公共的,我们将收到一个编译时错误,提示 pack1。 A 在 pack1 中不是公共的,不能从外部包访问。
类似地,成员或方法或接口被声明为公共的,因为我们可以从任何地方访问该成员。
修饰符 2:受保护的访问修饰符
此修饰符可应用于数据成员、方法和构造函数,但此修饰符不能应用于顶级类和接口。
成员被声明为受保护的,因为我们只能在当前包中访问该成员,但只能在外部包的子类中访问该成员。
执行:
例子
Java
// Java program to showcase the example
// of 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() 方法,并且它成功执行,因此从上面的例子中我们可以说我们可以通过以下任一方式访问当前包中的受保护方法使用父引用或子引用。
修饰符 3:私有访问修饰符
此修饰符不适用于顶级类或接口。它仅适用于类中的构造函数、方法和字段。
如果变量或方法或构造函数被声明为私有,那么我们只能从类内部访问它们,即从类外部我们无法访问它们。
例子:
Java
// Java program to showcase the example
// of private access modifier
// import required packages
import java.io.*;
import java.util.*;
// helper class
class A {
// helper method
private void m1() { System.out.println("GFG"); }
}
// driver class
class B {
// main method
public static void main(String[] args)
{
// creating an object of type class A
A a = new A();
// accessing the method m1()
a.m1();
}
}
修饰符 4:包(默认)访问修饰符
一个没有任何访问修饰符的类或方法或变量声明然后被认为它有一个包(默认)访问修饰符默认修饰符在同一个包内作为公共的,在包外作为私有的。如果一个类被声明为默认,那么我们只能在当前包中访问该类,即从外部包中我们不能访问它。因此,默认访问修饰符也称为包级访问修饰符。类似的规则也适用于Java的变量和方法。
例子:
Java
// Java Program to illustrate Package Level Access Modifier
// Importing utility classes
// Importing input output classes
import java.io.*;
import java.util.*;
// Main Class
class GFG {
// Declaring default variables that is
// having no access modifier
String s = "Geeksfor";
String s1 = "Geeks";
// Method 1
// To declare a default method
String fullName()
{
// Concatenation of strings
return s + s1;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating an object of main class(GFG)
// in the main() method
GFG g = new GFG();
// Calling method1 using class instance
// and printing the concation of strings
System.out.println(g.fullName());
}
}
GeeksforGeeks
最后,在完成所有四个访问修饰符之后,让我们总结一下它们之间的明显差异
Public Access Modifier | Private Access Modifier | Protected access modifier | Package access modifier |
---|---|---|---|
This modifier is applicable for both top-level classes and interfaces. | This modifier is not applicable for both top-level classes and interfaces. | This modifier is not applicable for both top-level classes and interfaces. | This modifier is applicable for both top-level classes and interfaces. |
Public members can be accessed from the child class of the same package. | Private members cannot be accessed from the child class of the same package. | Protected members can be accessed from the child class of the same package. | Package members can be accessed from the child class of the same package. |
Public member can be accessed from non-child classes of the same package. | Private members cannot be accessed from non-child classes of the same package. | Protected member can be accessed from non-child classes of the same package. | Package member can be accessed from non-child class of the same package. |
Public members can be accessed from the child class of outside package. | Private members cannot be accessed from the child class of outside package. | Protected members can be accessed from the child class of the outside package, but we should use child reference only. | Package members cannot be accessed from the child class of outside package. |
Public members can be accessed from non-child class of outside package. | Private members cannot be accessed from non-child class of outside package. | Protected members cannot be accessed from the non-child class of outside package. | Package members cannot be accessed from non-child class of outside package. |
Public modifier is the most accessible modifier among all modifiers. | Private modifier is the most restricted modifier among all modifiers. | Protected modifier is more accessible than the package and private modifier but less accessible than public modifier. | Package modifier is more restricted than the public and protected modifier but less restricted than the private modifier. |