6

Possible Duplicate:
Why doesn't Java allow overriding of static methods?

Is there any legitimate reason why one would want a derived class to override hide a static method of the base class?

Community
  • 1
  • 1
Saket
  • 45,521
  • 12
  • 59
  • 79
  • 5
    They can't be overriden. They can be hidden. – BalusC Sep 23 '11 at 04:26
  • Take a look at this thread - http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods – KV Prajapati Sep 23 '11 at 04:27
  • @BalusC Semantics... Regardless, I don't think anyone has actually answered the question. – NullUserException Sep 23 '11 at 04:30
  • 2
    ... errr, that wasn't the question. My understanding: *would it make sense to override if it was possible* – Andreas Dolk Sep 23 '11 at 04:31
  • And why are people voting to close this as a duplicate of *that* question? Re-read the question, it has nothing to do with that (regardless of the wrong terminology) – NullUserException Sep 23 '11 at 04:32
  • @NullUserException: feel free to answer then (note: I didn't vote for close, that's also beyond me, those questions are definitely not the same, OP only has to fix the wrong terminology, that's also basically where my comment was all about) – BalusC Sep 23 '11 at 04:39
  • 1
    @BalusC I would if I could think of an answer. – NullUserException Sep 23 '11 at 04:40
  • @NullUserExceptionఠ_ఠ - I will vote to re-open in case it gets closed to make up for the wrong close vote :) – CoolBeans Sep 23 '11 at 04:43
  • Guys, may be the way the question was put forward sounded inappropriate. But, I understand its rather termed as 'hidden' (thanks BalusC for the correction there!) than 'overriden'. What I really wanted to know, and as is different from the considered-dup question, is what are the real-life use cases for doing so (and why the language allowed this at all). And thanks @NUE for the clarifications above. – Saket Sep 23 '11 at 05:17
  • As to terminology, check the tutorial: http://download.oracle.com/javase/tutorial/java/IandI/override.html – BalusC Sep 23 '11 at 05:48
  • BTW: You can make a static method final so a derived method cannot hide it. You get an error like `m() in Main.B cannot override m() in Main.A; overridden method is static final` – Peter Lawrey Sep 23 '11 at 07:50

2 Answers2

6

Terminology aside, static methods in Java do have a kind of overriding relation, implied by binary compatibility section 13.4.12. If T extends S, S declared m(), T.m() can refer to a method in T or S, depending on if m() is declared in T; and it's ok to add or remove m() from T, without breaking any code calling T.m(). (This implies JVM invokestatic instruction does a sort of dynamic method lookup up the super class chain)

However, this is nothing but trouble. It is really dangerous if the meaning of T.m() silently changes because now it's pointing to a different method. (Instance methods shall inherit contracts so that's not a problem; there's no such understanding in static methods.)

So this "feature" should never be used; the language shouldn't have enabled it to begin with.

The good practice: If we call T.m(), m() must be declared in T; and it should never be removed from T without removing all T.m() first.

irreputable
  • 44,725
  • 9
  • 65
  • 93
  • Fully agree that they should never be used. However, it is *possible*. Can you think of any legitimate real world examples? In my Java career I've never encountered them. – BalusC Sep 23 '11 at 04:47
  • I have never encountered them either, so it must be rare people "override" static methods. I think I first learned it when investigating another tricky question http://stackoverflow.com/questions/6643648 – irreputable Sep 23 '11 at 05:01
  • +1 for the explanation. But this is exactly what I wish to know as to why possible at all, if this doesn't seem logical (at least to most of us). – Saket Sep 23 '11 at 05:19
  • 1
    @Saket some ideas seemed brilliant in the beginning; but all ideas must be tested by the passage of time. In this case, the feature clearly didn't pass the test; it should be removed if we have a time machine. – irreputable Sep 23 '11 at 05:24
  • @BalusC: one scenario would be that a particular class turns out to be a special case of a more general thing, so it gets a new, more abstract superclass and static methods are moved to the new super class, so that new code can access them without referring to, what is now one specific implementation. Old code will still find the method, however, the same would work if you keep a stub method at the old place, delegating to the new. – Holger Aug 30 '16 at 09:57
4

Static methods cannot be overriden

In order to override a method, the method must first be inherited. If the method is not inherited there is no chance for overriding. Therefore, you can never override a private method as they are not inherited.

Gregory Nozik
  • 3,296
  • 3
  • 32
  • 47