java overriding static method

Can we override a static method? – Yes, WE CAN! 🙂 but we don;t get the same effect as with non-static ones.

Static methods are not associated with an instance of a class. thus you cannot really override them, but:

class Main {
    public static void main(String args[]) {
            SuperClass a = new SubClass();
            SubClass b = new SubClass ();
            a.imStatic();
            b.imStatic();
            a.callStatic();
            b.callStatic();
    }
}
abstract class SuperClass {
    public static void imStatic() {
        System.out.println("I am superclass");
    }

    public void callStatic() {
        imStatic(); //no late binding here; always calls A.foo()
    }
}

class SubClass extends SuperClass {
    public static void imStatic() {
        System.out.println("I am subclass");
    }
}

the we get:

I am superclass
I am subclass
I am superclass
I am superclass

So what really matter is the type of the caller class, not the instance.