This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Originally posted by padmaratna kamble: hello friends ........ please tell me we always use List l=new ArrayList(); and Collection c=new ArrayList();
why we not always use
ArrayList a=new ArrayList();
Imagine situation. You have a class which uses 20 times your once created ArrayList instance. In a newer JVM appears better implementation for List interface and you would like to use it, but you have to rewrite whole class! If only you had used interface, you could rewrite only one line.
For better design of your api is very, very useful to access classes via their interfaces.
In the above code, the instance of ArrayList is referred by ArrayLiist reference variable. Now suppose there is a slight change in ur requirement. Instead of ArrayList u want to use LinkedList, then u have to make changes at two different places in ur code. An ArrayList reference variable can't refer ny LinkedList instances.
U code will become..
change at one place only. Only change ArrayList constructor to LinkedList constructor like this.
This is the obvious advantage with second code.
One more thing in case 2, polymorphic feature of java is used which is not in case 1.