Dissecting the Program
- Line 2-4 imports the collection framework classes and interfaces reside in the
java.utilpackage.
- The class hierarchy of the
ArrayListis shown above. We observe thatArrayListimplementsList,CollectionandIterableinterfaces. TheCollectionandIterableinterfaces define the common behaviors of all the collection implementations. InterfaceCollectiondefines how to add and remove an element into the collection. InterfaceIterabledefines a mechanism to iterate or transverse through all the elements of a collection. Instead of using the interfaceCollectiondirectly, it is more common to use one of its sub-interfaces,List(an ordered list supporting indexed access),Set(no duplicate elements) orQueue(FIFO, priority queues). - In line 8, we construct an
ArrayListinstance, and upcast it to theListinterface. This is possible as theArrayListimplementsListinterface. Remember that a good program operates on the interfaces instead of an actual implementation. The Collection Framework provides a set of interfaces so that you can program on these interfaces instead of the actual implementation.

https://www.ntu.edu.sg/home/ehchua/programming/java/J5c_Collection.html