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.

Bax
  • 4,260
  • 5
  • 43
  • 65
  • 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): http://stackoverflow.com/questions/26963828/reducing-the-visibility-of-a-static-method. About fields: they are not polymorphic (regardless if they are static or not so there is no point in limiting range of their changes). – Pshemo Dec 18 '15 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'. – Bax Dec 18 '15 at 23:42
  • I preferred the edit, because I think this question is about the [access modifier restrictions when hiding a method](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.3), 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)?_" – jaco0646 Dec 19 '15 at 00:29
  • Jon Skeet hypothesizes in a [comment](http://stackoverflow.com/questions/10297252/strange-case-of-static-method-override-in-java#comment13250789_10297296), "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. – jaco0646 Dec 19 '15 at 00: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](http://stackoverflow.com/questions/25169175/why-are-class-static-methods-inherited-but-not-interface-static-methods). – jaco0646 Dec 19 '15 at 00:49

1 Answers1

-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

Ruslan Neruka
  • 463
  • 5
  • 12
  • Question is about static methods. – chrylis -cautiouslyoptimistic- Dec 19 '15 at 03:41
  • 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. – Ruslan Neruka Dec 19 '15 at 03:51
  • 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. – Ruslan Neruka Dec 19 '15 at 03:57
  • "in Java static and non static method have same inheritance mechanism" - NO! See [here](http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods), [here](http://stackoverflow.com/questions/16617408/overriding-static-methods-in-java), [here](http://stackoverflow.com/questions/4987127/inheritance-in-static-methods) and [here](http://stackoverflow.com/questions/10291949/are-static-methods-inherited-in-java). There are many more examples around on the internet. Static methods DO NOT OVERRIDE. Just try putting `@Override` on your sub-class method. – DuncanKinnear Dec 21 '15 at 20:25