Class Relationships and Simple Diagrams
A quick sketch of is-a and has-a relationships communicates a design — and catches a backwards relationship before it becomes code.
By the end of this lesson, you can
- Read a simple diagram distinguishing an is-a relationship from a has-a relationship
- Diagram the relationships between a small set of classes
- Explain why sketching a design first can catch a mistake before any code exists
Why it matters
This module has built up several classes — LibraryBook,
ReferenceBook, Account, Member, Animal and its subclasses —
connected by two kinds of relationship. As a design grows, holding all
of it in your head from code alone gets harder. A quick diagram
communicates the shape of a design at a glance, the same way pseudocode
communicates an algorithm before any Python exists.
Mental model
Every relationship in this module is one of two kinds, and a diagram only needs to show which:
[LibraryBook]
^
| is-a
|
[ReferenceBook][Member] --has-a--> [Account]The direction matters. ReferenceBook is a LibraryBook — every
reference book is a library book, specialized to refuse borrowing. The
reverse isn’t true: an ordinary LibraryBook isn’t necessarily a
ReferenceBook. Member has a Account — a member owns one, but
isn’t one itself.
Diagram it
| Classes | Relationship | Diagram |
|---|---|---|
| LibraryBook, ReferenceBook | is-a | LibraryBook <- ReferenceBook |
| Animal, Dog / Cat / Bird | is-a | Animal <- Dog, Cat, Bird (each separately) |
| Member, Account | has-a | Member --has-a--> Account |
Multiple classes can point at the same parent in an is-a diagram — Dog,
Cat, and Bird are each their own is-a Animal relationship, drawn
separately, not chained to each other.
Check your understanding
Or reveal the answer without checking
Answer:has-a, Car composed of Engine
A car owning an engine, without being a specialized kind of engine, is exactly a has-a relationship — composition, not inheritance.
Or reveal the answer without checking
Answer:is-a only holds one way: the specific class must always be a valid example of the general one, and checking the reverse direction catches an arrow drawn backwards
Checking both directions — does A is-a B, does B is-a A — reveals which one is actually true. Only the direction from specific to general holds; confirming the reverse fails is what catches a backwards arrow.
Practice: warm-up
For each pair, decide whether it’s is-a or has-a, and sketch which
direction the relationship points: (1) a SportsCar and a Car, (2) a
Playlist and a Song, (3) a Square and a Shape.
Stuck? Reveal one hint at a time.
Hint 1
For each pair, ask: is every example of the first thing also a valid example of the second? If yes, it's is-a.
Hint 2
A playlist contains songs — does that sound like ownership (has-a) or specialization (is-a)?
Reveal the classification
Try the problem yourself before reading this. There is often more than one reasonable approach — treat this as one worked example, not the only correct answer.
SportsCar / Car -> is-a (Car <- SportsCar)
Playlist / Song -> has-a (Playlist --has-a--> Song)
Square / Shape -> is-a (Shape <- Square)Practice: apply it
A teammate sketched this diagram for a Manager and an Employee:
[Manager]
^
| is-a
|
[Employee]
Or reveal the answer without checking
Answer:No — it says every Employee is-a Manager, which isn't true; it should be drawn the other way, Employee at the top
This diagram's arrow points from Employee up to Manager, which reads as 'Employee is-a Manager' — backwards. Every manager is an employee, but not every employee is a manager, so Manager is the specific class and belongs at the bottom, pointing up at Employee.
Modification challenge: redraw the diagram with Employee at the
top and Manager at the bottom, pointing up at it — matching the
direction this lesson’s LibraryBook / ReferenceBook example used.
Summary
- A class diagram sketches classes as boxes and relationships as labeled arrows — is-a for inheritance, has-a for composition.
- An is-a arrow always points from the specific class to the general one, never the reverse.
- Checking a relationship in both directions — does A is-a B, does B is-a A — is what catches an arrow drawn backwards.
- Sketching a design before writing code catches a relationship mistake while it’s still a quick fix, not a class hierarchy to untangle.