I am trying one simple method overloading in java but I cannot figure out the behaviour of "null" when passed as argument . Or I guess I am missing some theoretical aspect of overloading . Below is my code :
public class MethodOverloadNull {
String met1(Object o) {
System.out.println("In Objecct O");
return "hello";
}
String met1(String o) {
System.out.println("In String");
return "hello";
}
public static void main(String[] args) {
MethodOverloadNull obj=new MethodOverloadNull();
obj.met1(null);
}
}
Output is : In String
.
But when I commented out overloaded String arg method I am getting output as : In Object O
.
public class MethodOverloadNull {
String met1(Object o) {
System.out.println("In Object O");
return "hello";
}
/*
String met1(String o) { // commented this method
System.out.println("In String");
return "hello";
}
*/
public static void main(String[] args) {
MethodOverloadNull obj=new MethodOverloadNull();
obj.met1(null);
}
}
Output : In Object O
Why is this behaviour ???