I have this following code and I need some clarification regarding the overloading concept.
class Overload{
static void print(Object o){
System.out.println("object");
}
static void print(String s){
System.out.println("string");
}
public static void main(String...args){
print("Hello");
}
}
when I execute this, the output is String. Eventhough object is the super class, why does it display string instead of object?
thanks in advance
The rule in this case is:
Stringis more specific thanObject, that’s whyprint(String)is used. For instance, try withprint(null)in yourmain, you will see find out that such code will not even compile, since the compiler will have no way to distinguish whethernullis aStringor anObjectCheck out Oracle Overloading Tutorial