Reputation: 49402
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
?
Upvotes: 5
Views: 973
Reputation: 1502226
Yes - because Double
is more specific than Object
. There's a conversion from Double
to Object
, 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:
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
So here, any invocation of display(Double doub)
could be handled by display(Object obj)
but not the other way round.
Upvotes: 6