Originally posted by storm zcm: hello. I want to know what's the difference between List<String> test = new ArrayList<String>(); and ArrayList<String> test = new ArrayList<String>();
Case #1 That is polymorphic assignment: test is the reference variable of the interface List that refers to the object of the class ArrayList that implements List interface.
Using list interface you are only able to call methods that are declared in the interface List; this is sometimes called narrowing the scope, but good to avail the methods polymorphically.
Case #2 Reference variable of class ArrayList refers to the object of the its class. So using the reference variable you can call all the methods that are available with that class including implemented methods of the List interface.
In both the cases we have type safe list, the List can only contain String objects, nothing else. Or in other words the add() method will only accept String object.