I have a static method defined in a base class, I want to override this method in its child class, is it possible?
I tried this but it did not work as I expected. Wh
Static method calls are resolved on compile time (no dynamic dispatch).
class main {
public static void main(String args[]) {
A a = new B();
B b = new B();
a.foo();
b.foo();
a.callMe();
b.callMe();
}
}
abstract class A {
public static void foo() {
System.out.println("I am superclass");
}
public void callMe() {
foo(); //no late binding here; always calls A.foo()
}
}
class B extends A {
public static void foo() {
System.out.println("I am subclass");
}
}
gives
I am superclass
I am subclass
I am superclass
I am superclass