r/learnjava • u/[deleted] • Dec 02 '19
A question about List and ArrayList in Java
Is there a good reason why you would create a polymorphic type of ArrayList from List?
for example: List<String>someRandomList = new ArrayList<>();
Im trying to understand.
all comments are appreciated!
42
Upvotes
5
25
u/anthropaedic Dec 02 '19
List is an interface, and ArrayList is an implementing class. It's almost always preferable to code against the interface and not the implementation. This way, if you need to change the implementation later, it won't break consumers who code against the interface