Type conversion and method overloading

This is a 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);
}
}

Conclusion:

Inside double

Can someone explain to me why using it instead Object?

the overloaded method is called with the parameter Double
+5
source share
1 answer

Yes - because it’s Doublemore specific than Object. There is a conversion from Doubleto Object, but not vice versa, which makes it more specific.

See section 15.12.2.5 JLS for more information. The details are pretty hard to complete, but it helps:

, , , , , , .

, display(Double doub) display(Object obj), .

+6

All Articles