Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
Do not introduce ambiguity while overloading (see [MET50-J. Avoid ambiguous uses of overloading]) and use overloaded methods sparingly \[[Tutorials 2010|AA. Bibliography#TutorialsReferences#Tutorials 10]\] as they can make code much less readable.

...

Wiki Markup
At compile time, the type of the object array is {{List}}. The expected output is {{ArrayList}}, {{ArrayList}}, {{LinkedList}} and {{List is not recognized}} ( because {{java.util.Vector}} does not inherit from {{java.util.List}}). The actual output is {{ArrayList}} followed by three instances of {{List is not recognized}}. The cause of this unexpected behavior is that overloaded method invocations are affected _only_ by the compile time type of their arguments: {{ArrayList}} for the first invocation and {{List}} for the others. Do not use overloading where overriding would be natural \[[Bloch 2008|AA. Bibliography#BlochReferences#Bloch 08]\].

Compliant Solution

This compliant solution uses a single display method and instanceof to distinguish between different types. As expected, the output is ArrayList, ArrayList, LinkedList, List is not recognized.

...

Wiki Markup
\[[API 2006|AA. Bibliography#APIReferences#API 06]\] [Interface Collection|http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collection.html]
\[[Bloch 2008|AA. Bibliography#BlochReferences#Bloch 08]\] Item 41: Use overloading judiciously
\[[Tutorials 2010|AA. Bibliography#TutorialsReferences#Tutorials 10]\] [Defining Methods|http://download.oracle.com/javase/tutorial/java/javaOO/methods.html]

...