Java基类中超类的受保护方法会怎样? [英] What happens to protected method of a super class in base class in Java?
本文介绍了Java基类中超类的受保护方法会怎样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在package1
中有一个A
类,而B
在package2
中,它继承了A
. A
包含方法m1
,它是protected
.现在我的疑问是,当我在另一个也是package2
的类C
中创建B
的对象时,B
的对象无法访问方法m1
为何?下面是我的代码
I have a A
class in package1
and B
is in package2
which inherits A
. A
contains method m1
which is protected
. Now my doubt is when I create an object of B
in another class C
which is also package2
, the object of B
is unable to access method m1
why? Below is my code
package com.package1;
public class A {
protected void m1(){
System.out.println("I'm protectd method of A");
}
}
package com.package2;
import com.package1.A;
public class B extends A {
public static void main(String[] args) {
B b = new B();
b.m1(); // b object able to access m1
}
}
package com.package2;
public class C {
public static void main(String[] args) {
System.out.println("Hi hello");
B b = new B();
b.m1(); //The method m1() from the type A is not visible
}
}
超类的受保护方法在子类中是否私有?
Do protected method of super class become private in subclass?
推荐答案
查看全文