IS-A and HAS-A
easy 40 minIS-A says that an object is a kind of something and can stand in for it.
Watch lesson4:45
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When the choice comes up
| You hear | Think | Say |
|---|---|---|
| Users can become admins | Begins and ends | A role that the user has |
| Premium accounts with insurance and cashback | Features combine | A list of features, not subclasses |
| Our queue should reuse ArrayList | Wants code, not the type | A queue has a list |
| Pieces on a board move differently | Kinds for life, same question | Piece as a contract. A pawn that is promoted is replaced |
| A report in PDF, sent by mail | Two independent reasons | A report has a format and a sender |
Saying it in the room
Interviewer
You made ElectricCar a subclass of Car. Why?
You
Let me test it. It is a car for life, and a garage uses it as a car. But refuel() makes no sense on it, so a promise is broken.
Interviewer
So?
You
The car has a drive with
topUp(). Petrol and electric are kinds of drive, and a hybrid has two. Car itself promises only what all cars do.