[英]Java - Why Child in another package cannot access parent's protected method through parent reference?
I have two classes in two different packages:我在两个不同的包中有两个类:
package package1;
public class ParentClass {
public void testPublic() {
}
protected void testProtected() {
}
}
package package2;
import package1.ParentClass;
public class ChildClass extends ParentClass {
void test() {
ParentClass par = new ParentClass();
par.testProtected(); // Line 1 : ERROR: testProtected() has protected access in ParentClass
testProtected(); // Line 2 : No error
ChildClass ch = new ChildClass();
ch.testProtected(); // Line 3 : No ERROR
testProtected(); // Line 4 : No error
}
}
I am able to understand why there is NO ERROR in calling testProtected() -- Line 2
since ChildClass
sees this method as it inherits from ParentClass
.我能够理解为什么在调用
testProtected() -- Line 2
,因为ChildClass
看到此方法是从ParentClass
继承的。
And somehow able to understand why it is throwing ERROR in calling par.testProtected() -- Line 1
, since par
is a different object, and current object is not having access to other object's parent's protected method.并且以某种方式能够理解为什么它在调用
par.testProtected() -- Line 1
时抛出错误- 第 1 行,因为par
是不同的 object,并且当前 object 无法访问其他对象的父级受保护方法。
But how it is possible for an object of ChildClass to access this same method ch.testProtected() -- Line 3
(other object's parent's protected method) when the reference type is of ChildClass only?但是,当引用类型仅为 ChildClass 时,ChildClass 的 object 如何访问相同的方法
ch.testProtected() -- Line 3
(其他对象的父对象的受保护方法)?
The error you're seeing makes total sense.您看到的错误完全有道理。
When you do the following当您执行以下操作时
ParentClass par = new ParentClass();
par.testProtected();
you're trying to access testProtected
as if it were part of the public API of ParentClass
, that's it, you're calling from another package a protected method of ParentClass
on an instance of that class .您正在尝试访问
testProtected
,就好像它是ParentClass
的公共 API 的一部分一样,就是这样,您正在从另一个 package调用该 ZA2F2ED4F8EBC2CBB4C21A 实例上的ParentClass
的受保护方法。 It doesn't matter it is being called inside an extending class, all that matters is that it's being called from another package.在扩展的 class 中调用它并不重要,重要的是它是从另一个 package 调用的。
Now, if you do现在,如果你这样做
ChildClass ch = new ChildClass();
ch.testProtected(); // Line 3
testProtected(); // Line 4
you will not see any errors.你不会看到任何错误。 Why?
为什么?
In line 3 you're accessing a method upon an instance of class ChildClass
inside ChildClass
, so that's legal.在第 3 行中,您正在访问
ChildClass
内 class ChildClass
实例的方法,因此这是合法的。 Even if you would have had a private method childMethod
in ChildClass
you could have done ch.childMethod()
.即使您在
ChildClass
中有一个私有方法childMethod
,您也可以完成ch.childMethod()
。
In line 4 you're accessing testProtected
through the this
reference (implicitly), it means it calls the method through the ChildClass
, which, again is legal.在第 4 行中,您正在通过
this
引用(隐式)访问testProtected
,这意味着它通过ChildClass
调用该方法,这也是合法的。
See this related post: What is the difference between public, protected, package-private and private in Java?请参阅此相关帖子: Java 中的 public、protected、package-private 和 private 有什么区别? (hint: last row in the described table)
(提示:所述表中的最后一行)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.