AlgoViz

IS-A and HAS-A

easy 40 min

IS-A says that an object is a kind of something and can stand in for it.

Watch lesson4:45

Remember it

The few things worth knowing cold, and how to make them stick.

Memory hook

Is it one for life, or does it have one for now?

Eye colour and backpack: you are born with the one and cannot swap it. You pack the other and change its contents every morning.

Cheat sheet

IS-A
extends, implements. Hollow triangle
HAS-A
field. Line or diamond
USES-A
parameter. Dashed arrow
IS-A test
for life, promises kept, used as the type
Feature names in a class
parts
Start and end date
role, HAS-A
n on-off features
2 to the power n subclasses, or n parts
IS-A in tables
single, class or concrete table

Spot it when you see

  • Class names with two adjectives
  • A method that makes no sense on a child
  • Becomes, for now, optional, replaceable
  • Extends a collection

Tricks that stick

  • IS-A is **DNA**: you cannot change your parents.
  • HAS-A is **building bricks**: snap on, take off.
  • IS-A is a tree that goes up. HAS-A is a row of parts beside you.
  • Ask: could it lose it? Then it has it.

Do not forget

  • Trusting the English sentence
  • Extending for code only
  • Exposing the part so that callers reach through
  • A part that needs its whole
  • Turning every field into an interface