5

I am confused by the behaviour of the static method when it is overridden in the subclass.

Below is the code:

public class SuperClass {

    public static void staticMethod() {
        System.out.println("SuperClass: inside staticMethod");
    }
}

public class SubClass extends SuperClass {

//overriding the static method
    public static void staticMethod() {
        System.out.println("SubClass: inside staticMethod");
    }
}


public class CheckClass {

    public static void main(String[] args) {

        SuperClass superClassWithSuperCons = new SuperClass();
        SuperClass superClassWithSubCons = new SubClass();
        SubClass subClassWithSubCons = new SubClass();

        superClassWithSuperCons.staticMethod();
        superClassWithSubCons.staticMethod();
        subClassWithSubCons.staticMethod();

    }
}


Below is the output which we are getting :

    1) SuperClass: inside staticMethod
    2) SuperClass: inside staticMethod
    3) SubClass: inside staticMethod

Why static method of superclass gets called here in the second case?

If method is not static, then according to polymorphism, method of the subclass is called when subclass object is passed on runtime.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kshitij Jain
  • 1,793
  • 5
  • 19
  • 30

4 Answers4

7

static method resolution is always based on the Reference type.
The code

superClassWithSuperCons.staticMethod();
superClassWithSubCons.staticMethod();
subClassWithSubCons.staticMethod();

is converted to this after compilation

SuperClass.staticMethod();
SuperClass.staticMethod();
SubClass.staticMethod();

Accroding to this it is the call to SuperClass method not the subclass method.So you are getting the output of SuperClass method.

Prabhaker A
  • 8,317
  • 1
  • 18
  • 24
4

A method declared static cannot be overridden but can be re-declared. That's the answer. A static method is not associated with any instance of a class so the concept is not applicable. Try the same with not static and you'll see the difference.

Your question is duplicated actually: Why doesn't Java allow overriding of static methods?

Community
  • 1
  • 1
Ivan Voroshilin
  • 5,233
  • 3
  • 32
  • 61
3

Interesting question. I'm not familiar with the underlying mechanisms, but it seems that for static methods, the declared type (in your middle example, SuperClass), not the actual type SubClass is considered for resolving the method call. It actually makes sense because you're not looking at the actual instance of an object when calling a static function.

Nicolas78
  • 5,124
  • 1
  • 23
  • 41
  • 1
    right, and subclass only 'hides' static methods in superclass. [Here is a tutorial](http://docs.oracle.com/javase/tutorial/java/IandI/override.html) – grape_mao Sep 14 '13 at 17:23
0

Static methods are redefined not overridden...

If you try to override the static method then it will be treated as a non overridden method of the sub class.

Static methods will be called based on the type of the reference not the object created.

For more information visit the page. http://javaunturnedtopics.blogspot.in/2016/07/static-methods-are-redefined-not.html

Tarun Bedi
  • 61
  • 2