受保护的关键字是用于限制变量、方法和构造函数的访问范围的关键字。它是Java中访问修饰符的一种类型。它们用于区分方法、变量、构造函数和类的范围。 Java中有4种类型的访问修饰符,它们是:
广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试一旦变量或方法被标记为受保护,就只能通过以下方法访问:
受保护的关键字就像公共和私有关键字的组合,因为引入它们是为了访问类外部的变量(这在私有关键字的情况下是不可能的),并维护只有某些方法可以继承相同的变量。
受保护的关键字是用其前缀为“protected”的关键字来声明的。我们首先在名为“MyClass”的类中声明 protected 关键字,如下所示:
class MyClass { protected String name = "Katy"; protected int token= 55; } public class SubClass extends MyClass { public static void main(String[] args) { SubClass obj = new SubClass(); System.out.println(obj.name + "'s token number is: " + obj.token); } }
这里的类“SubClass”扩展了“MyClass”,因此可以通过创建 SubClass 的对象并调用变量来使用 protected 关键字。
输出:
受保护的关键字只能在成员级别使用,即在函数外部声明的非静态内部类。 protected 关键字与 private 不同,因为它们可以在类外部和另一个包的子类中访问。
使用受保护关键字的一些限制是:
让我们通过一些示例来更好地理解受保护关键字的概念。
这里我们尝试从“package1”的父类中调用关键字。 “ProtectedExample2”是在“package2”中创建的,这里调用了关键字“disp”。但是代码将无法访问该关键字,因为子类没有从主类继承其值,并且会抛出异常,如图所示。
代码:
package com.package1; public class Example { protected String disp="Printing message from protected variable from package1"; } //Create new package as com.package2 //Create new class as ProtectedExample2 package com.package2; import com.package1.Example; public class ProtectedExample2 { public static void main(String[] args) { ProtectedExample2 a=new ProtectedExample2(); System.out.println(a.disp); } }
输出:
在此示例中,我们尝试访问受保护的“ProtectedExample5”类。这会导致编译错误。
代码:
protected class ProtectedExample5 { void display() { System.out.println("Try to access outer protected class"); } public static void main(String[] args) { ProtectedExample5 p=new ProtectedExample5(); p.display(); } }
输出:
在下面的示例中,我们首先创建一个名为“com.package1”的包,并创建一个名为“Example”的新类。在这里我们声明我们的关键字“disp”受到保护。我们将尝试使用“Example1”类来显示这个受保护的关键字。为此,首先需要创建父类“Example1”的对象,然后打印分配给关键字“disp”的值。
代码:
package com.package1; public class Example { protected String disp="Printing message from protected variable from package1"; } class Example1 { public static void main(String[] args) { Example obj=new Example(); System.out.println(obj.disp); } }
输出:
Using the same code as above, we shall see how to call the protected keyword by creating a different package, “package2”. A protected keyword can be accessed only through inheritance from package1; hence “ProtectedExample2” is extended from “Example”. In a similar way as the first example, we have to create an object of the class “ProtectedExample2” in order to access the protected keyword from package “com.package1”.
Code:
package com.package2; import com.package1.Example; public class ProtectedExample2 extends Example{ public static void main(String[] args) { ProtectedExample2 a=new ProtectedExample2(); System.out.println(a.disp); } }
Output:
Here the class is declared as protected inside the inherited class “Example5”. Also, a protected class called “Example” is declared outside the function but in the same package. When an object of “Example5” is created and the protected class “disp()” is called, we can observe that the overridden method is called instead of the outside class. This is because we shall not be able to import “com.package1” and its class “Example” since it is not visible and causes a compilation error.
Code:
//Create a file by Example.java package com.package1; class Example { protected void disp() { System.out.println("Printing from protected class in the outside function"); } } //Create a class by the name Example5.java public class Example5 extends Example { protected void disp() { System.out.println("Accessing the overriden function"); } public static void main(String[] args) { Example5 exp=new Example5(); exp.disp(); } }
Output:
The importance of protected keyword in java is:
As shown in the above examples, we choose protected keywords depending on the access level we require at the code level. They help greatly in cases where the same variable or class needs to be accessed from other inherited methods in the program. A parent-child relationship is always present between the parent class and its sub-classes which are using the protected keyword.
以上是Java 中受保护的关键字的详细内容。更多信息请关注PHP中文网其他相关文章!