Why does Java restrict the access modifier of a hi

2019-04-08 15:15发布

问题:

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