I have 3 classes GrandParent
, Parent
and Child
, where
Child extends Parent
and Parent extends GrandParent
public class Main {
void test(GrandParent gp){System.out.println("GrandParent");}
void test(Parent p){System.out.println("Parent");}
public static void main(String args[]){
GrandParent obj = new Child();
Main mainObj = new Main();
mainObj.test(obj); // This calls test(GrandParent gp)
mainObj.test(new Child()); // This calss test(Parent gp)
}
}
In above code in the 2 calls to test()
method both with Child
object calls different methods. In one it's doing compile-time and in other run-time binding. This sounds little weird to me. How would you explain this?