top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why Static Method in Java can't be overridden?

+3 votes
407 views
Why Static Method in Java can't be overridden?
posted Aug 3, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Static methods cannot be overridden because method overriding only occurs in the context of dynamic (i.e. runtime) lookup of methods. Static methods (by their name) are looked up statically (i.e. at compile-time).

Method overriding happens in the type of subtype polymorphism that exists in languages like Java and C++. A variable that can point to objects of class Foo, can also point (at runtime) to objects of dynamic (runtime) class SubFoo (where SubFoo is a subclass of Foo). If a call is made to a method of Foo on this variable, and it happens at runtime that this variable points to an object of class SubFoo, and SubFoo also has a version of this method, it will actually run SubFoo's version of the method, and not Foo's, even though at compile time we may have no idea that the variable could point to an object of class SubFoo.

This is the gist of dynamic lookup and method overriding. The actual method to be called is not determined until runtime, and a subclass can implement a method with the same signature as a superclass method, and that method will be executed in place of the method in the superclass if the method is called on an instance of the subclass, without the caller needing to know the subclass exists.

For "static methods" in Java and C++, there is no dynamism -- the exact method to call is determined at compile time. You explicitly specify the name of the class whose static method you want to execute, or it is implied by the scope. In Java, there's also a misleading syntax where you can "call a static method on an instance" (where on the left side of the dot you can have an expression computed at runtime). Despite how it looks, the actual value of the left side expression computed at runtime is not used in determining what method is called. Instead, only the static (compile-time) type of the expression on the left side is used to determine the method to call, at compile time. That is why the same method is called even if the left side evaluates to null or to an instance of a subclass that has the same static method.

(In some object-oriented languages, like Smalltalk and Objective-C, they have "class methods" that are looked up at runtime. In these languages, class methods are simply methods of the class object itself, and are inherited like instance methods. You can compute a class object at runtime and call a method on it, without knowing which method it will look up to at runtime. In these languages, then, class methods can be overridden.)

class MyClass {
   static void foo() {}
 }

 class MySubclass extends MyClass {
   void foo() {}
 }

This instance method cannot override the static method from MyClass

answer Aug 3, 2015 by Karthick.c
...