ankur8819
Member • May 26, 2012
Overloading Behaviour in Java
public class testOverload {
public void xyz(Object o){
System.out.println("o");
}
public void xyz(Exception o){
System.out.println("exc");
}
public void xyz(RuntimeException o){
System.out.println("runtime");
}
public static void main(String[] args) {
testOverload t= new testOverload();
t.xyz(null);
}
}
The above method gives runtime as Output.Can anyone explain this behaviour of JRE.
null is a type of Object so it should give "o" as output.
More specifically if you include one more overloaded version of xyz method as
public void xyz(NullPointerException o){
System.out.println("null");
}
then it would print null which suggests that its treating null as an object of NullPointerException Class.
Any comments on this?
public void xyz(Object o){
System.out.println("o");
}
public void xyz(Exception o){
System.out.println("exc");
}
public void xyz(RuntimeException o){
System.out.println("runtime");
}
public static void main(String[] args) {
testOverload t= new testOverload();
t.xyz(null);
}
}
The above method gives runtime as Output.Can anyone explain this behaviour of JRE.
null is a type of Object so it should give "o" as output.
More specifically if you include one more overloaded version of xyz method as
public void xyz(NullPointerException o){
System.out.println("null");
}
then it would print null which suggests that its treating null as an object of NullPointerException Class.
Any comments on this?