Card 04/ 08
All 8 cards
ConceptDifficulty: Intermediate1 min
Declare the Interface, Create the Implementation
A method that used to take an ArrayList needs to accept a list built somewhere else, and the somewhere else produces a LinkedList. The change is one word, in eleven files.
List<String> refs = new ArrayList<>();
void report(List<String> refs) { }The declared type is a statement about what you need. List says "something ordered that I can walk", which is true of every list ever written. ArrayList says "this exact class", which is more than you needed and less than you can get.
| Declared as | Accepts | Changing the implementation |
|---|---|---|
List<String> | Any list | One word, in one place |
ArrayList<String> | Only ArrayList and its subclasses | Every declaration and every signature |
This matters most at method boundaries. A parameter typed ArrayList refuses List.of(...), refuses Arrays.asList(...), and refuses whatever a library hands you — none of which is a decision anyone made on purpose.
So name the interface everywhere except immediately after new. It costs nothing, and it is what lets the choice of implementation stay a local decision rather than a signature everybody has to agree with.