This is the sample code :
public class OverloadingExample {
public void display(Object obj){
System.out.println("Inside object");
}
public void display(Double doub){
System.out.println("Inside double");
}
public static void main(String args[]){
new OverloadingExample().display(null);
}
}
Output:
Inside double
Can anyone please explain me why the overloaded method with Double parameter is called instead of that with Object ?
Yes – because
Doubleis more specific thanObject. There’s a conversion fromDoubletoObject, but not the other way round, which is what makes it more specific.See section 15.12.2.5 of the JLS for more information. The details are pretty hard to follow, but this helps:
So here, any invocation of
display(Double doub)could be handled bydisplay(Object obj)but not the other way round.