polymorphism doubt
In the program below , why is it that the seconf testPoly is executed?
public class First {
}
class Second extends First{
}
class Third extends Second{
public static void testPoly(First a,First b){
System.out.println("@@@@@@@@@@@@@@@1");
}
public static void testPoly(First a,Second b){
System.out.println("@@@@@@@@@@@@@@@2");
}
public static void main(String args[]){
Third third = new Third();
testPoly(third,third);
}
}
[525 byte] By [
ivinjacoba] at [2007-10-2 19:37:38]

Because "First, Second" is more specific on the argument type than "First, First" and still matches the arguments provided. if it wouldn't work that way, you could never overload a method that takes an Object reference as an argument, unless you use primitives.
A hint: this applies however only to the static (=compile-time) type of the aargument, not its run-time type.
class First {
}
class Second extends First{
}
public class Third extends Second{
public static void testPoly(First a,First b){
System.out.println("@@@@@@@@@@@@@@@1");
}
public static void testPoly(First a,Second b){
System.out.println("@@@@@@@@@@@@@@@2");
}
public static void main(String args[]){
Third third = new Third();
First first = third;
testPoly(third,third);
testPoly(third,first);
}
}
java Third
@@@@@@@@@@@@@@@2
@@@@@@@@@@@@@@@1