英文:
Using protected method of the parent class in another package
问题
I have Tadpole
class that extends Frog
, as follows:
1: package animal;
2: public class Frog {
3: protected void ribbit() { }
4: void jump() { }
5: }
1: package other;
2: import animal.*;
3: public class Tadpole extends Frog {
4: public static void main(String[] args) {
5: Tadpole t = new Tadpole();
6: t.ribbit();
7: t.jump(); // doesn't compile due to default access
8: Frog f = new Tadpole();
9: f.ribbit(); // doesn't compile
10: f.jump(); // doesn't compile due to default access
11: } }
Lines 7 and 10 do not compile due to the package-private access of the jump() method.
I understand that upcasting of Tadpole
class into Frog
class on line 8 will allow the parent instance member (the ribbit method) to be accessed from the subclass since the method is not overwritten.
Protected methods should be able to be accessed from a subclass whether it's in the same package or different package right? If so, why doesn't line 9 compile?
英文:
I have Tadpole
class that extends Frog
, as follows:
1: package animal;
2: public class Frog {
3: protected void ribbit() { }
4: void jump() { }
5: }
1: package other;
2: import animal.*;
3: public class Tadpole extends Frog {
4: public static void main(String[] args) {
5: Tadpole t = new Tadpole();
6: t.ribbit();
7: t.jump(); // doesn't compile due to default access
8: Frog f = new Tadpole();
9: f.ribbit(); // doesn't compile
10: f.jump(); // doesn't compile due to default access
11: } }
Lines 7 and 10 do not compile due to the package-private access of the jump() method.
I understand that upcasting of Tadpole
class into Frog
class on line 8 will allow the parent instance member (the ribbit method) to be accessed from the subclass since the method is not overwritten.
Protected methods should be able to be accessed from a subclass whether it's in the same package or different package right? If so, why doesn't line 9 compile?
答案1
得分: 0
一个 protected
方法是一个可以被调用的方法:
- 要么在声明它的类
Frog
内部 - 要么在任何继承自
Frog
的子类的实例源代码内部 - 要么最终在同一个包内
在你的具体情况下,你都不在这些情况中。
棘手的部分可能在于你正在Tadpole
的源代码内部编写调用,它是Frog
的子类(所以你可以说,嘿,我在第二种情况中)。
但实际上并不是这样。
你并没有从Tadpole
类的实例方法中调用这个方法,而是在一个static
上下文(一个main
方法)中,你在创建一个Frog f
的实例,并尝试从该实例f
中访问该方法。你可能在任何其他地方,情况都是一样的。
如果方法 ribbit()
是私有的,你不会感到惊讶你无法执行 f.ribbit()
。但同样也适用于 protected
,如果你以相同的方式思考的话。
英文:
A protected
method is a method that can be invoked:
- Either inside the class
Frog
that declares it - Or inside the instance source code of any subclass of
Frog
- Or ultimately from within the same package
In your specific case, you are in neither of these cases.
The tricky part may be that you are writing your call inside the source code of Tadpole
, that is a subclass of Frog
(so you may say hey, I'm in the case two).
But actually it's not like that.
You are not invoking the method from an instance method of the class Tadpole
, rather you are in a static
context (a main
method), where you are creating an instance of Frog f
, and you're trying to access the method from that instance f
. You may be anywhere else, and it would be the same thing.
If the method ribbit()
was private, you wouldn't have been suprised that you couldn't do f.ribbit()
. But the same goes for protected
, if you think the same way.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论