Java protected关键字
Java protected关键字是访问修饰符。可以将其分配给变量,方法,构造函数和内部类。
Java protected要记住的要点
protected访问修饰符可在包中访问。但是,它也可以在包外部但仅通过继承进行访问。
我们无法将protected对象分配给外部类和接口。
如果使任何构造函数protected,则不能从包外部创建该类的实例。
如果您要覆盖任何方法,则被覆盖的方法(即在子类中声明的方法)必须没有更多限制。
根据上一点,如果将protected分配给任何方法或变量,则只能使用公共或protected的访问修饰符将该方法或变量覆盖为子类。
protected关键字的示例
示例1
让我们看一个示例来确定protected的变量是否可访问或不在包装之外。
package com.java;
public class A {
protected String msg="try to access the protected variable outside the package";
}
//save by ProtectedExample1.java
package com.lidihuo;
import com.java.A;
public class ProtectedExample1 {
public static void main(String[] args) {
A a=new A();
System.out.println(a.msg);
}
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field A.msg is not visible
示例2
让我们看一个示例,以确定是否可以在类外部和程序包内部访问protected的变量。
class A {
protected String msg="try to access the protected variable outside the class within the package";
}
public class ProtectedExample2 {
public static void main(String[] args) {
A a=new A();
System.out.println(a.msg);
}
}
输出:
try to access the protected variable outside the class within the package
示例3
让我们看一个示例,以确定在程序包外部是否可以访问protected的方法。
//save by A.java
package com.java;
public class A {
protected void msg()
{
System.out.println("try to access the protected method outside the package ");
}
}
//save by ProtectedExample3.java
package com.lidihuo;
import com.java.A;
public class ProtectedExample3 {
public static void main(String[] args) {
A a=new A();
a.msg();
}
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method msg() from the type A is not visible
示例4
让我们看一个示例,以确定是否可以使用继承在包外部访问protected的方法。
//save by A.java
package com.java;
public class A {
protected void msg()
{
System.out.println("try to access the protected method outside the package using inheritance");
}
}
//save by ProtectedExample4.java
package com.lidihuo;
import com.java.A;
public class ProtectedExample4 extends A {
public static void main(String[] args) {
ProtectedExample4 a=new ProtectedExample4();
a.msg();
}
}
输出:
try to access the protected method outside the package using inheritance
示例5
让我们看一个示例,以确定我们是否将保护分配给外部类。
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();
}
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
示例6
让我们看一个示例,以确定我们是否从类外部创建protected的构造方法的实例。
//save by A.java
package com.java;
public class A
{
String msg;
protected A(String msg)
{
this.msg=msg;
}
public void display()
{
System.out.println(msg);
}
}
//save by ProtectedExample6.java
package com.lidihuo;
import com.java.A;
public class ProtectedExample6 {
public static void main(String[] args) {
A a=new A("try to create the instance of protected constructor outside the package");
a.display();
}
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor A(String) is not visibles
示例7
让我们看一个示例,以确定是否使用protected的访问修饰符将protected的方法重写为子类。
//save by A.java
class A
{
protected void msg()
{
System.out.println("try it");
}
}
//save by ProtectedExample7.java
class ProtectedExample7 extends A {
protected void msg()
{
System.out.println("try to access the overridden method");
}
public static void main(String[] args) {
ProtectedExample7 p=new ProtectedExample7();
p.msg();
}
}
输出:
try to access the overridden method
示例8
让我们看一个示例,以确定是否使用私有访问修饰符将protected的方法重写为子类。
class A
{
protected void msg()
{
System.out.println("try it");
}
}
class ProtectedExample8 extends A {
private void msg()
{
System.out.println("try to access the overridden method");
}
public static void main(String[] args) {
ProtectedExample8 p=new ProtectedExample8();
p.msg();
}
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot reduce the visibility of the inherited method from A
示例9
让我们看一个示例,以确定是否使用默认访问修饰符将protected的方法重写为子类。
class A
{
protected void msg()
{
System.out.println("try it");
}
}
class ProtectedExample8 extends A {
void msg()
{
System.out.println("try to access the overridden method");
}
public static void main(String[] args) {
ProtectedExample9 p=new ProtectedExample9();
p.msg();
}
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot reduce the visibility of the inherited method from A
示例10
让我们看一个示例,以确定是否使用公共访问修饰符将protected的方法重写为子类。
class A
{
protected void msg()
{
System.out.println("try it");
}
}
class ProtectedExample10 extends A {
public void msg()
{
System.out.println("try to access the overridden method");
}
public static void main(String[] args) {
ProtectedExample10 p=new ProtectedExample10();
p.msg();
}
}
输出:
try to access the overridden method