[英]protected can't access in different package subclass why?
Error: m1() has protected access in A
错误: m1() has protected access in A
When I try to using superclass reference variable ( A obj=new B()
) 当我尝试使用超类引用变量( A obj=new B()
)时
This is a first class in package pkg1; 这是pkg1包中的头等舱;
package pkg1;
public class A {
protected void m1() {
System.out.println("protected method");
}
}
This is a second class which is in another package pkg2 importing pkg1; 这是第二类,在另一个软件包pkg2中导入pkg1。
package pkg2;
import pkg1.A;
public class B extends A {
@Override
protected void m1() {
System.out.println("override m1");
}
public static void main(String ar[]) {
A obj=new B();
obj.m1();
}
}
I feel like you're still slightly confused with why protected m1()
isn't visible. 我觉得您仍然对为什么看不到protected m1()
感到有些困惑。
You understand that main
is a method in B
and B
is a subclass of A
, therefore you feel like it should be accessible. 您了解main
是B
的方法,而B
是A
的子类,因此您认为应该可以访问它。
The key is obj
is cast to be interpreted of type A
. 关键是将obj
强制转换为类型A
解释。 Then you try to call instance method m1
from static context in B
(see this link above for why this is important). 然后,您尝试调用实例方法 m1
从静态上下文中B
(见这个上面为什么这是重要的一环)。 By class A
definition, m1
is accessible through: 按照A
类A
定义,可以通过以下方式访问m1
:
//example package pkg1; public class C { protected void m1() { System.out.println("protected method"); } public static void callM1(C anInstanceOfC){ anInstanceOfC.m1(); } }
package pkg2; import pkg1.C; public class D extends C { @Override public void m1() { System.out.println("override m1"); } public static void main(String ar[]) { C obj=new D(); C.callM1(obj);//output would be "override m1" } }
//example package pkg1; public class C { protected void m1() { System.out.println("protected method"); } public void m2() { m1();//call to instance method m1 } public static void callM1(C anInstanceOfC){ anInstanceOfC.m2(); } }
package pkg2; import pkg1.C; public class D extends C { @Override public void m1() { System.out.println("override m1"); } public void m3() { //super.m2() m2();//call to superclass method } public static void main(String ar[]) { D obj=new D(); D.m3(); // output "override m1" } }
protected keyword means that you can access protected-defined data from child classes. protected关键字意味着您可以从子类访问受保护的定义的数据。 In example, you try to access protected data from non-child static context. 例如,您尝试从非子级静态上下文访问受保护的数据。 You should probably try this: 您可能应该尝试这样:
package pkg2;
import pkg1.A;
public class B extends A {
@Override
public void m1() {
System.out.println("override m1");
}
public static void main(String ar[]) {
B obj=new B();
obj.m1();
}
}
The access level of m1() is protected. m1()的访问级别受保护。 Protected methods can only be accessed by subclasses or by other classes in the same package . 受保护的方法只能由子类或同一包中的其他类访问。 From your static 'main' method, you can not call a protected instance method of a class in a different package. 从静态的 “ main”方法中,您不能在其他程序包中调用类的受保护实例方法。 If class B was in the same package as class A, then you will have no error and your code would work fine. 如果B类与A类位于同一包中,那么您将没有错误,并且代码可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.