Why Doesn't Java Allow Overriding of Static Methods

Why doesn't Java allow overriding of static methods?

Overriding depends on having an instance of a class. The point of polymorphism is that you can subclass a class and the objects implementing those subclasses will have different behaviors for the same methods defined in the superclass (and overridden in the subclasses). A static method is not associated with any instance of a class so the concept is not applicable.

There were two considerations driving Java's design that impacted this. One was a concern with performance: there had been a lot of criticism of Smalltalk about it being too slow (garbage collection and polymorphic calls being part of that) and Java's creators were determined to avoid that. Another was the decision that the target audience for Java was C++ developers. Making static methods work the way they do had the benefit of familiarity for C++ programmers and was also very fast, because there's no need to wait until runtime to figure out which method to call.

Why doesn't the compiler complain when I try to override a static method?

You didn't override anything here. To see for yourself, Try putting @Override annotation before public static void a() in class B and Java will throw an error.

You just defined a function in class B called a(), which is distinct (no relation whatsoever) from the function a() in class A.

But Because B.a() has the same name as a function in the parent class, it hides A.a() [As pointed by Eng. Fouad]. At runtime, the compiler uses the actual class of the declared reference to determine which method to run. For example,

B b = new B();
b.a() //prints B.a()

A a = (A)b;
a.a() //print A.a(). Uses the declared reference's class to find the method.

You cannot override static methods in Java. Remember static methods and fields are associated with the class, not with the objects. (Although, in some languages like Smalltalk, this is possible).

I found some good answers here: Why doesn't Java allow overriding of static methods?

If static methods can't be overridden, how its working here (For Java)?

First of all there are different mechanisms involved here: Overriding and Shadowing (also called hiding).

1)
Static methods cannot be overriden as they are attached to the class they are defined in. However, you can shadow/hide a static method as you are doing with your Parent/Child class. This means, the method gets replaced in the Child class but is still available from the Parent class.

It gets more obvious that you are not overriding when you are calling the static methods from instances of those classes (and not using the Class.staticMethod() invocation).

Parent parent = new Parent();
Child child1 = new Child();
Parent child2 = new Child();

parent.StaticMethod();
child1.StaticMethod();
child2.StaticMethod();

the output is

Static method from Parent
Static method from Child
Static method from Parent

The answer is the dispatch of the methods. You can grab the source code here

2)
The dispatch finds the method on the Parent class. There is no dynamic dispatch as that the runtime type is used to find the method handle. It uses the compile time type. Remind: Calling static methods from instances is considered bad practice since things like above can happen and are easy to be overlooked.

3)
With finalyou declare that the method cannot be overridden neither shadowed/hidden.

Behaviour of final static method

Static methods cannot be overridden but they can be hidden. The ts() method of B is not overriding(not subject to polymorphism) the ts() of A but it will hide it. If you call ts() in B (NOT A.ts() or B.ts() ... just ts()), the one of B will be called and not A. Since this is not subjected to polymorphism, the call ts() in A will never be redirected to the one in B.

The keyword final will disable the method from being hidden. So they cannot be hidden and an attempt to do so will result in a compiler error.

Hope this helps.



Related Topics



Leave a reply



Submit