Why Java restricts access modifier to hide method

When hiding a static field, there are no restrictions on the level of access to this field in the subclass; it can even be non-stationary and another type of data.

On the other hand, when hiding a static method, a static method from a subclass that hides the static method from the superclass can provide access, but no less than a hidden method.

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

PS The question is only out of curiosity.

+5
source share
1 answer

Because in a subclass, you override not the private method of the superclass, but the shadow fields. Regarding the installation of a wider access level - you can always write something like

public void sublcassMethod() { supersecretSuperclassMethod(); } 

therefore, it makes no sense to restrict overrides with wider access at the language level - such a restriction can be easily implemented

-2
source

Source: https://habr.com/ru/post/1238680/


All Articles