Why does Java restrict the access modifier of a hiding method [closed]

余生颓废 提交于 2019-12-06 03:30:58

问题


When hiding a static field, there's no restriction on what access level does the field have in subclass, it can be even non-static and of other data type.

On the other side, when hiding a static method, the static method from subclass that hides the static method from superclass can allow more, but not less, access than the hidden method.

AFAIK, static method linking is anyway done at compile time, so why is there such a restriction ?

P.S. The question is just out of curiosity.


回答1:


Because in subclass you override non private superclass method but shadow fields. As for setting broader access level - you always could write something like

public void sublcassMethod() {
    supersecretSuperclassMethod();
}

so there is no sense to restrict overriding with more broader access at language level - such restriction can be easy committed



来源:https://stackoverflow.com/questions/34365337/why-does-java-restrict-the-access-modifier-of-a-hiding-method

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