5

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.

5
  • 4
    Jon's suspicions (I didn't mark it as duplicate since answer simply confirms it based on JLS, while your question is more about why JLS is designed that way): stackoverflow.com/questions/26963828/…. About fields: they are not polymorphic (regardless if they are static or not so there is no point in limiting range of their changes). Commented Dec 18, 2015 at 23:23
  • 2
    Restored to pre-edited state. Just don't want people to understand the question as 'Why for fields is permitted and for methods is not'. Commented Dec 18, 2015 at 23:42
  • I preferred the edit, because I think this question is about the access modifier restrictions when hiding a method, in which case the static field example is misleading. I would phrase the question as, "Why does Java restrict the access modifier of a hiding method (since hiding is not polymorphic)?" Commented Dec 19, 2015 at 0:29
  • Jon Skeet hypothesizes in a comment, "that it's trying to avoid a situation where you have an explicit call to Subclass.foo() which is a private method which would normally resolve to Superclass.foo()." But I'm unable to see what problem that scenario presents. Commented Dec 19, 2015 at 0:38
  • 1
    It's interesting to note that when Java 8 added static methods to interfaces, this issue was avoided because those static methods are un-inheritable. Commented Dec 19, 2015 at 0:49

1 Answer 1

-2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Question is about static methods.
yes, i know. But eclipse in such situation will tell us "- Cannot reduce the visibility of the inherited method from Superclass". So i think, inheritance mechanism the same no matter static or non static method is.
don't forget that you can call static method on object. So i think to omit misunderstanding which static method call, in Java static and non static method have same inheritance mechanism.
"in Java static and non static method have same inheritance mechanism" - NO! See here, here, here and here. There are many more examples around on the internet. Static methods DO NOT OVERRIDE. Just try putting @Override on your sub-class method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.