This is also somewhat esoteric. I've been programming in Java for years,
and I didn't know that
. For *most* cases, its better not to use
overloading, but instead use similarly named methods that describe there
differences, for example:
public void showObject(Object o) { System.out.println("Object"); }
public void showString(String s) { System.out.println("String"); }
I would say the primary exception is when the input will be treated
exactly the same regardless of type (println for example), in which case
each overload should take an Object, or any type of primitive.
Here "null" can be assigned to String, so when you have overloaded
methods , compiler searches for matching(specific) method first, in
this case first preference to the method which has String as an
argument. Object is generic(base class for all other classes) so when
there is no match "show(Object o)" will be called.
[Note]: Object class is the root class, so when there is no match ,
when all possibilities failed , method will be called which has
argument of type Object(final option).
Ex: Switch cases and the default. when all cases failed , default
block will be executed.
Thanks,
Patel.