0

I'm a beginner in Java. From my understanding, when we use any Collections in Java, we declare data type between the tag. However, I'm confused why we would use an Interface as data type for a collection, for example a list as shown below.

List<Type> list3 = new ArrayList<Type>();
List<Interface> list3 = new ArrayList<Interface>();

Appreciate your help. Thank you.

json27
  • 89
  • 1
  • 1
  • 5

1 Answers1

0

From reference: http://www.javapractices.com/topic/TopicAction.do?Id=26

In general, references to objects should be as generic as possible. The user of such a reference will be protected from possible changes to the underlying implementation class. The ripple effects of such a change are limited to the single line of code which creates the object, and will not propagate any further.

High level references (interfaces or abstract classes) are utilized instead of low level references (concrete classes) to minimize Ripple Effect.

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45