Is protected method in super class visible in sub class in a different package? [duplicate]

邮差的信 提交于 2019-11-28 14:17:17

The Java Language Specification says

A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.

What that means is that if you're writing a subclass outside the package of the original class, each object can call the superclass's protected methods on itself, but not on other objects.

In your example, because s is a different object from this, you can't call s.fun(). But the object would be able to call the fun method on itself - with this.fun() or just fun().

Protected methods are only visible to subclasses from the inside. If you create a new instance of SuperClass, you are accessing it from the outside.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!