Hi, have a look at this code:
public class StaticInherit {
public static int num=100;
public static int myMethod() {
return num=num+10;
}
public static void main(
String args[]) {
B myB= new B();
System.out.println(myB.myMethod());
}
}
class B extends StaticInherit {
public static int myMehtod() {
return num=num+80;
}
}
OUPUT:
110
How come the output is 110? According to this article
Hiding Vs Overriding,
for static methods, the compiler only uses the declared type of the reference. That's what we mean when we say a static method does not have run-time polymorphism. In above example, the reference type is B i.e. B myB= new B(); Then why is compiler invoking superclass version of the method? Thanks