friends,
I am trying to understand the following program. It's from Dan's sample chapter on Method Overloading.
class GFC200 {}
class GFC201 {
static void m(Object x) {System.out.print("Object");}
static void m(
String x) {System.out.print("String");}
static void m(GFC200 x) {System.out.print("GFC200");}
public static void main(String[] args) {m(null);}
}
a. Prints: Object
b. Prints: String
c. Prints: GFC200
d. Compile-time error
e. Run-time error
f. None of the above
Answer : d. compile-time error
The description, as given by Dan, says that the argument 'null' could be converted to any of the three methods having arguments, 'Object', 'String' or 'GFC200'. But as none of them are more specific, it will end in compile-time error.
I also saw another sample question where it speaks about two methods having arguments as 'Object' and 'String'. In this case, if we pass 'null', the method having argument as 'String' will receive 'null' argument and it will compile without error.
If that's the case what's wrong with the above question and why can't the method that contains 'String' argument accept the 'null'? This is little confusing me.
Would appreciate, if anyone could help me explain this.
Thanks in advance
Joe