当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Can't access protected method from child class

问题描述:

This question already has an answer here:

  • Why subclass in another package cannot access a protected method?

    6 answers

网友答案:

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

If you want to access the B.f(), you should have the C class defined in the same package as B.

网友答案:

No because the compiler does not know whether b is an instance of B, C, or other external classes.

Suppose there is a class D such that D extends A in another package. If b is an instance of D (the compiler does not know) b.f() should be forbidden if the access is protected (can't be accessed by classes in other packages).

分享给朋友:
您可能感兴趣的文章:
随机阅读: