Overloading a method is hard : a common pitfall
About 1 min read
As I said in my equality article, overloading in Java1 is resolved by the static type of the argument, not the run-time type.
It’s a generic problem of most compiled OO languages since usually overloading resolution happens at compile-time and not at runtime.
Now, that militates for the well known idiom :
Never overload a method with one that has the same number of parameters.
Actually, it should be enough to overload a method with one that accept parameters that are not inheritance-related : String and Number would be OK, but MyClass and Object would not.
-
It’s not really a Java-ism, it’s the same in other languages, such as C++ . ↩