11

I am trying to understand why we can't override static and final methods. I do not get the purpose behind it.

Universal Electricity
  • 775
  • 1
  • 12
  • 26
  • Dup: http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods – Furquan Khan Nov 29 '13 at 09:16
  • Wikipedia has a whole quite decent page on ["final" in java](https://en.wikipedia.org/wiki/Final_%28Java%29) that covers also the underlying question on the purpose of final (why would we want something that can NOT be overridden?) – Paolo Falabella Nov 29 '13 at 09:17
  • This question is perfectly answered [here][1] [1]: http://stackoverflow.com/questions/2223386/why-doesnt-java-allow-overriding-of-static-methods hope it helped :) – Lukas Warsitz Nov 29 '13 at 09:17
  • Please use google or the search functionality of SO before posting redundant content. – mwhs Nov 29 '13 at 09:18
  • Please read http://stackoverflow.com/help/how-to-ask – Infinite Recursion Nov 29 '13 at 09:23
  • Also I would suggest you to put a more informative title. The present title will not impress or attract many readers! – Rahul Tripathi Nov 29 '13 at 09:46
  • Why aren't aquired reputation points being revoked after a question has been marked as duplicate? It would be the duty of every SO user to check if such a simple question hasn't been answered before instead of producing redundant content. – mwhs Nov 29 '13 at 10:01
  • By simple I mean that it is actually really simple to find out that the question has been asked before. SO actually suggests it to the author and the reader. No big deal... – mwhs Nov 29 '13 at 10:31
  • Coming up soon: "Why we cannot access private variables?" – Overv Nov 29 '13 at 13:45

5 Answers5

53

final methods can't be overriden, because that's what final is designed to do: it's a sign saying "do not override this".

static methods can't be overriden, because they are never called in a polymorphic way: when you call SomeClass.foo() it will always be the foo method of SomeClass, no matter if there is another ExtendedSomeClass that has a much groovier foo method.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
5

final is used to avoid overriding. And a static method is not associated with any instance of a class so the concept is not applicable.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
uhs
  • 838
  • 1
  • 7
  • 21
3

The reason for not overriding static method is that Static methods are treated as global by the JVM so there are not bound to an object instance at all. Similarly final methods cant be overriddent because when you say a method as final then it mean you are saying to the JVM that this method cannot be overridden.

The wiki has a very important misconception about final. Do read that!

A final method cannot be overridden or hidden by subclasses.[2] This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.[3]

A common misconception is that declaring a class or method as final improves efficiency by allowing the compiler to directly insert the method wherever it is called (see inline expansion). But because the method is loaded at runtime, compilers are unable to do this. Only the runtime environment and JIT compiler know exactly which classes have been loaded, and so only they are able to make decisions about when to inline, whether or not the method is final.[4]

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

Static methods are covered here.

Final methods cannot be overridden because the purpose of the "final" keyword is to prevent overriding.

Community
  • 1
  • 1
Domi
  • 22,151
  • 15
  • 92
  • 122
0

Final cannot be overridden because that is the purpose of the keyword, something that cannot be changed or overridden.

The purpose of inheritance and polymorphism is to have objects of same class implementing methods (not the names but the code in the methods) in different ways. And static methods cannot be accessed by objects because they are a part of the class not the instance. So there is no purpose to overriding the Static methods. And you can have a static method in subclass by the same name but that won’t be an overridden method.

If you haven't yet read about Inheritance and Polymorphism which are both features of Java, you should and also try to write the code in the question so that SO users can answer with an example.

Nick Div
  • 5,338
  • 12
  • 65
  • 127