I am new to java, I have seen in the code at many places where my seniors have declared as
List myList = new ArrayList(); (option1)
Instead of
ArrayList myList = new ArrayList(); (option2)
Can you please tell me why people use Option1, is there any advantages?
If we use option2, do we miss out any advantages or features?
The advantage of using option1, ie,
List myList = new ArrayList();is to have polymorphic behaviour with respect to methods. Say, for example, you can have a method which takes arguments of type List,In this method
lstcan be of typeLinked List,ArrayListorCopyOnWriteArrayList.