📜  Java的受保护与包访问修饰符

📅  最后修改于: 2021-09-14 02:14:44             🧑  作者: Mango

每当我们编写我们的类时,我们必须向 JVM 提供一些关于我们的类的信息,比如这个类是否可以从任何地方访问,是否可以创建子类,是否可以创建对象等。我们可以通过在Java使用适当的关键字访问修饰符来指定此信息。因此访问修饰符用于设置类、方法和其他成员的可访问性。

修饰符 1:受保护的访问修饰符

此修饰符可应用于数据成员、方法和构造函数,但此修饰符不能应用于顶级类和接口。成员被声明为受保护的,因为我们只能在当前包中访问该成员,但只能在外部包的子类中访问该成员。

例子:

Java
// Java Program to show Protected keyword
 
// Importing input output classes
import java.io.*;
// Importing utility classes
import java.util.*;
 
// Class 1
// Parent class
class Parent {
 
    // declaring a protected method m1()
    protected void print() { System.out.println("GFG"); }
}
 
// Class 2
// Child class which is extending Parent class
class Child extends Parent {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an object of parent class
        // using parent reference
        Parent p = new Parent();
 
        /// calling the print() method of Parent class
        p.print();
 
        // Creating an object of child class
        // using child reference
        Child c = new Child();
 
        // Calling the print() method of Parent class
        c.print();
 
        // Creating an object of child class
        // using parent reference
        Parent cp = new Child();
 
        // Calling the print method over this object
        cp.print();
    }
}


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());
    }
}


输出

GFG
GFG
GFG

输出说明:

在上面的例子中,我们使用父引用和子引用创建了三个对象,并在其上调用了 m1() 方法,并且它成功执行,因此从上面的例子中我们可以说我们可以通过以下任一方式访问当前包中的受保护方法使用父引用或子引用。

修饰符 2:包(默认)访问修饰符

一个没有任何访问修饰符的类或方法或变量声明然后被认为它有一个包(默认)访问修饰符默认修饰符在同一个包内作为公共的,在包外作为私有的。如果一个类被声明为默认,那么我们只能在当前包中访问该类,即从外部包中我们不能访问它。因此,默认的访问改性剂也被称为包水平访问修饰符。类似的规则也适用于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

最后,在完成它们之后,让我们总结一下它们之间的明显差异

                 Package access modifier                                   Protected access modifier 
This modifier is applicable to both top-level classes and interface This modifier is not applicable to both top-level classes and interface
We cannot access this modifier from the child class of the outside package. We can access this modifier from the child class of the outside package, but we should use child reference only. 
This modifier is more restricted than the protected modifier. This modifier is more accessible than the package level modifier.