Card 01/ 08
All 8 cards
ConceptDifficulty: Intermediate1 min
Naming What a Thing Can Do Without Saying How
A reporting method needs to sort things. It does not care whether they are invoices, employees or files — only that any two of them can be put in an order.
An interface is a name for that capability and nothing else. It lists method signatures with no bodies, and any class can declare that it supplies them.
interface Archivable {
String archiveKey();
boolean isExpired();
}A class that writes implements Archivable is promising those two methods exist. The compiler enforces the promise: leave one out and the class will not build.
| The interface decides | The class decides |
|---|---|
| Which methods exist, and their signatures | What each method does |
| That anything implementing it can be used interchangeably | What state it keeps to do the job |
So code written against Archivable works with every class that ever implements it, including ones written after that code shipped. That is the payoff: the calling code names a capability rather than a list of types, and the list of types is free to grow.