Method
getPlayerCardHashMap
returns an instance of a concrete collection type161 return currentCard.isPlayable(card);
162 }
163
164 public HashMap<Player, List<Card>> getPlayerCardHashMap() {165 return playerCardHashMap;166 }167
168
169 protected void setCurrentCard(Card card){
Description
Concrete collection types (such as ArrayList
, HashMap
, etc.) should not be used in a public
method's signature.
Java encourages the use of abstract types/interfaces at the API boundary over concrete types. This helps one design generic APIs that are easy to modify and extend.
Although designing generic APIs is generally preferable, one should especially emphasize their use over concrete types when elements of the collection API are involved. This is because almost all non-trivial Java applications depend heavily on abstract types defined in Java's collection framework.
Bad Practice
// Return type is `ArrayList` instead of `List`.
public ArrayList<Integer> method() {
// ..rest of the code
}
// Parameter type is `HashMap` instead of `Map`.
public void methodWithParams(HashMap<String, String> demo) {
// ..rest of the code
}
Recommended
Consider using abstract types in return values and parameters of public
methods.
public List<Integer> method() {
// ..rest of the code
}
public void methodWithParams(Map<String, String> demo) {
// ..rest of the code
}
References
- StackOverflow - Why we should use interface instead of concrete types?
- Oracle Java Documentation - Polymorphism