public class Test{
public static void abc(String s) {
System.out.println("String");
}
public static void abc(Object s) {
System.out.println("OBject");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
abc(null);
}}
Output-String
I am Beginner in java,I am confused about the output of the above program. Please explain me the reason of the output.
Early Binding (Binding most specific method at compile time).
When you overload methods, most specific method will be choosen. In your case the order of choosing is String >Object (since null can be of any reference type).
In the hierarchy, String is more specific than Object. Hence string got chosen. In fact Object is the least specific among all the java objects
Here is the JLS for the same
http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2.5
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
..... [rules]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With