首页 > Java > java教程 > 正文

Java 中受保护的关键字

WBOY
发布: 2024-08-30 15:22:35
原创
235 人浏览过

受保护的关键字是用于限制变量、方法和构造函数的访问范围的关键字。它是Java中访问修饰符的一种类型。它们用于区分方法、变量、构造函数和类的范围。 Java中有4种类型的访问修饰符,它们是:

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试
  1. 默认关键字:它们只能在包内访问,不能在包外调用。顾名思义,当未提及访问说明符时,它会自动分配为默认值。
  2. 公共关键字:可以从程序中的任何位置访问它们。这意味着它可以在相同或不同的类以及相同或不同的包中使用。
  3. 私有关键字:它们将关键字限制在更高的级别,不允许从类本身之外的任何地方访问它们。
  4. 受保护关键字:在本文中,我们将了解更多有关受保护关键字的信息。

一旦变量或方法被标记为受保护,就只能通过以下方法访问:

  • 在声明它的同一个类中。
  • 来自与声明的类也位于同一包中的其他类。
  • 从声明的类继承的类,无论其包如何。

受保护的关键字就像公共和私有关键字的组合,因为引入它们是为了访问类外部的变量(这在私有关键字的情况下是不可能的),并维护只有某些方法可以继承相同的变量。

语法

受保护的关键字是用其前缀为“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 关键字。

输出:

 Java 中受保护的关键字

受保护的关键字只能在成员级别使用,即在函数外部声明的非静态内部类。 protected 关键字与 private 不同,因为它们可以在类外部和另一个包的子类中访问。

使用受保护关键字的一些限制是:

  • 它们不能用于将类声明为受保护。
  • 接口不能声明为受保护。
  • 只能通过继承来访问包外部。
  • 受保护的构造函数无法通过创建其实例在包外部访问。

Java 中受保护关键字的示例

让我们通过一些示例来更好地理解受保护关键字的概念。

1.调用 protected 关键字而不扩展父类

这里我们尝试从“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);
}
}
登录后复制

输出:

Java 中受保护的关键字

2.访问受保护的类

在此示例中,我们尝试访问受保护的“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();
}
}
登录后复制

输出:

Java 中受保护的关键字

3.显示来自同一包但不同类的受保护关键字

在下面的示例中,我们首先创建一个名为“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);
}
}
登录后复制

输出:

Java 中受保护的关键字

4. Displaying protected keyword from a different package

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:

Java 中受保护的关键字

5. Accessing a protected class by overriding to sub-class

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:

Java 中受保护的关键字

Importance of Protected Keyword

The importance of protected keyword in java is:

  • These keywords allow the classes or their variables to be inherited from their parent class which is not possible with any other restricted keyword such as private.
  • A protected keyword is the combination of a private keyword’s advantage and that of a public keyword. It eliminates the disadvantage of public keyword that the variable or class be accessible from anywhere in the program by restricting the scope.

Conclusion

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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!