Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: minor corrections

...

Do not introduce ambiguity while overloading (see MET50-JG. Avoid ambiguous uses of overloading), and use overloaded methods sparingly [Tutorials 20102008], because they can make code much less readable.

...

Code Block
bgColor#ccccff
class Overloader {
public class Overloader {
  private static String display(List<?> list) {
    return (
      list instanceof ArrayList ? "Arraylist" : 
      (list instanceof LinkedList ? "LinkedList" : 
      "List is not recognized")
    );
  }

  public static void main(String[] args) {
    // Single ArrayList
    System.out.println(display(new ArrayList<Integer>()));

    List<?>[] invokeAll = new List<?>[] {new ArrayList<Integer>(), 
    new LinkedList<String>(), new Vector<Integer>()};

    for (List<?> list : invokeAll) {
      System.out.println(display(list));
    }
  }
}

...

Applicability

Ambiguous uses of overloading can lead to unexpected results.

...

[API 2011] Interface Collection<E>
[Bloch 2008] Item 41: Use overloading judiciously
[Tutorials 20102008] Defining Methods

...