0

Why would I instantiate an ArrayList referring to the interface List like this:

List<T> mylist = new ArrayList<T>();

Instead of simply:

ArrayList<T> mylist = new ArrayList<T>();
ocram
  • 1,384
  • 6
  • 20
  • 36
  • you should read about upcasting and downcasting in java – Sarthak Mittal Nov 17 '15 at 18:25
  • List is not an abstract class, but interface. If you create a method that accepts some given instance of List type, it would also accept all List implementors (LinkedList, ArrayList, etc). However, if your method accept exactly ArrayList, the compiler prevents that method from handling any list, except for ArrayList (or classes derrived from ArrayList) – Nikolai Shevchenko Nov 17 '15 at 18:29

0 Answers0