Yes - because if you do not need certain functionality that is displayed only through a specific type, it is usually recommended to refer to a more general type. Thus, if you ever decide to use another implementation, you know that you are not attached to anything specifically with the current implementation. You can change one statement later:
List<String> list = new ArrayList<String>();
(say)
List<String> list = new LinkedList<String>();
and know that everything will compile. Of course, the behavior can change in terms of performance, thread safety, etc., but itβs all the same.
You also declare that you do not need any members that are related to ArrayList<String> , which may be important when reading the code later.
All this is especially true when it comes to choosing return data types and parameter types. The more specific you are about the return type, the less flexibility you should change later. The more specific you are about the type of parameters, the less flexibility you provide to your subscribers.
source share