☰
If you had seen my previous article, I have used something like this:
List<Parent> lst = new ArrayList<Parent>();
The main reason you'd do this is to decouple your code from a specific implementation of the interface. This is preferable because rest of the code only knows that data is of type List and hence you can switch between different implementations of the List interface with ease.
Imagine you are coding a large 3rd party library and you have decided to implement LinkedList.If your library relies heavily on accessing elements in these lists, then eventually you'll find that you've made a poor design decision; you'll realize that you should have used an ArrayList instead of a LinkedList. Assuming you have been programming to an interface, making such a change is easy. You would simply change the instance of List from,
List list = new LinkedList();
to
List list = new ArrayList();
and you know that this will work because you have written your code to follow the contract provided by the List interface.
Now think if you had implemented your library using LinkedList list = new LinkedList(), making changes to this wouldn't be easy, because there is no guarantee that the rest of your code doesn't make use of methods specific to the LinkedList class.
This is called programming to interface. It is a matter of design. This will be helpful as it will allow you to make implementation-specific changes later without breaking existing code.
Sincere thanks from CoreJavaGuru to the author for submitting the above article.