Versions Compared

Key

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

Java supports overloading methods and can distinguish between methods with different method signatures. This means that, with some qualifications, methods within a class can have the same name if they have different parameter lists. In method overloading, the determination of the method to be invoked at runtime is determined at compile time. Consequently, the overloaded method associated with the static type of the object is invoked, even when the runtime type differs for each invocation.

Wiki Markup
Do not introduce ambiguity while overloading (see [MET50-J. Avoid ambiguous uses of overloading]), and use overloaded methods sparingly \[[Tutorials 2010|AA. References#Tutorials 10]\], asbecause 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. References#Bloch 08]\].

...

Sound automated detection of violations is infeasible , because it would require determination of programmer intent. Heuristic techniques may be useful.

...