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?

11
  • 5
    They can't be overriden. They can be hidden. Commented Sep 23, 2011 at 4:26
  • Take a look at this thread - stackoverflow.com/questions/2223386/… Commented Sep 23, 2011 at 4:27
  • @BalusC Semantics... Regardless, I don't think anyone has actually answered the question. Commented Sep 23, 2011 at 4:30
  • 2
    ... errr, that wasn't the question. My understanding: would it make sense to override if it was possible Commented Sep 23, 2011 at 4:31
  • 1
    @BalusC I would if I could think of an answer. Commented Sep 23, 2011 at 4:40

2 Answers 2

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.

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

5 Comments

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.
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 stackoverflow.com/questions/6643648
+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 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.
@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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.