 
                            ...
| Wiki Markup | 
|---|
| This noncompliant code example shows how the programmer can confuse overloading with overriding. At compile time, the type of the object array is {{List}}. The expected output is {{ArrayList}}, {{LinkedList}} and {{List is not recognized}} ({{java.util.Vector}} does not inherit from {{java.util.List}}). However, in all three instances {{List is not recognized}} gets displayed. This happens because in overloading, the method invocations are not affected by the runtime types but only the compile time type ({{List}}). It is dangerous to implement overloading to tally with overriding, more so, because the latter is characterized by inheritance unlike the former. \[[Bloch 08|AA. Java References#Bloch 08]\] | 
| Code Block | ||
|---|---|---|
| 
 | ||
| 
public class Overloader {
  private static String display(ArrayList<Integer> a) {
    return "ArrayList";
  }
  private static String display(LinkedList<String> l) {
    return "LinkedList";
  }
  private static String display(List<?> l) {
    return "List is not recognized";
  }
  public static void main(String[] args) {
    // Array of lists
    List<?>[] invokeAll = new List<?>[] {new ArrayList<Integer>(), 
    new LinkedList<String>(), new Vector<Integer>()};
    for(List<?> i : invokeAll) {
      System.out.println(display(i));
    }
  }
} | 
...
| Wiki Markup | 
|---|
| This compliant solution uses a single {{display}} method and {{instanceof}} to distinguish between different types. TheAs expected, the output is {{ArrayList, LinkedList, List is not recognized}}, as expected. As a general rule, do not introduce ambiguity while using overloading so that the code is clean and easy to understand. \[[Bloch 08|AA. Java References#Bloch 08]\] | 
...