The reason is
Overloaded
method to call is based on
Reference type passed at
compile time.
Here, compiler look at object reference "a" as type of class A, it will try to find an overloaded method
m1(long i) in class A. Unfortunately the compiler can not find any method named "m1" accepts a parameter with type of "long" in class A.
We know, at compile time, compiler does not know to which kind of object "a" really points. Only at runtime, JVM will find out "a" really referenced to object of type
B instead of its parent
A.
Hope this will help.
Correct me if anything wrong.