Generalization
The is-a relationship - a hollow triangle pointing at the parent. When inheritance clarifies a model, and when it quietly wrecks one.
Generalization is UML's strongest relationship claim. Where an association says "these objects know about each other," generalization says "this thing is that thing." That claim buys you a lot when it is true and costs you dearly when it is not, so it deserves more scrutiny than any other line you draw on a class diagram.
Notation and meaning
Draw a solid line from the subtype to the supertype and put a hollow (unfilled) triangle at the supertype end. The triangle always points at the parent: CardPayment -> Payment. Getting the direction wrong reverses the meaning of the model, so treat it as non-negotiable notation, not a stylistic choice.
The formal semantics are stricter than "shares some code." Generalization asserts full substitutability: anywhere the model expects a Payment, an instance of CardPayment must work without the surrounding code noticing. This is the substitutability idea usually credited to Barbara Liskov — a subtype must honor every promise its supertype makes, so callers written against the parent stay correct when handed a child.
A subtype inherits three things from its supertype:
- Attributes —
Payment.amountexists on everyCardPayment. - Operations —
Payment.authorize()can be called on any subtype, possibly with specialized behavior. - Associations — if
Paymentis associated withOrder, thenCardPaymentparticipates in that same association. You never redraw the line for each child.
That third point is the one modelers forget most often, and it is where generalization earns its keep: one association at the supertype level replaces a fan of duplicate lines.
Abstract supertypes
Often the parent exists only to be specialized — no object is ever "just a Payment." Mark the class abstract by writing its name in italics (or {abstract} after the name where italics are impractical, such as whiteboards). Abstract operations are italicized the same way. An abstract supertype is a contract plus shared structure; the concrete subtypes are the only things that get instantiated. This is the shape behind Factory Method and most uses of the Open-Closed Principle: callers depend on the stable abstract parent while new subtypes arrive without touching existing code.
Generalization is not realization
Generalization connects like with like: class to class, or interface to interface. A class that conforms to an interface is a different relationship — realization — drawn as a dashed line with the same hollow triangle at the interface end.
| Generalization | Realization | |
|---|---|---|
| Line style | Solid, hollow triangle | Dashed, hollow triangle |
| Connects | Class to class, or interface to interface | Class (or component) to interface |
| What is inherited | Attributes, operations, associations, behavior | Nothing — only the obligation to implement the operations |
| Claim being made | Is-a, with implementation shared | Conforms-to, contract only |
The distinction matters because the two make different promises. Generalization shares implementation; realization shares only a signature set. In TypeScript terms, class Square extends Shape is generalization, class Square implements Drawable is realization, and interface Drawable extends Printable is generalization between interfaces.
The classic misuse: inheriting for reuse
The most common inheritance mistake is subclassing to borrow code when no is-a relationship exists. The canonical offender:
// Looks convenient: Stack gets add, get, remove for free.
class Stack<T> extends ArrayList<T> {
public void push(T item) { add(item); }
public T pop() { return remove(size() - 1); }
}
Stack<String> s = new Stack<>();
s.push("a");
s.push("b");
s.add(0, "z"); // inherited — and it just broke LIFO orderingA stack is not a list; it is a discipline over a list. By inheriting, you publish the entire ArrayList API as part of your contract, including operations that violate your own invariant. (The JDK's real java.util.Stack extends Vector and has exactly this flaw — it is preserved only for backward compatibility.) The correct model is a stack that has a private list — see composition — exposing only push, pop, and peek.
Squares, rectangles, and contracts
Real-world taxonomy is a poor guide to subtyping, because substitutability is about behavioral contracts, not vocabulary. Every mathematician agrees a square is a rectangle. Now model mutable shapes:
class Rectangle {
constructor(protected width: number, protected height: number) {}
setWidth(w: number) { this.width = w; }
setHeight(h: number) { this.height = h; }
area(): number { return this.width * this.height; }
}
class Square extends Rectangle {
setWidth(w: number) { this.width = w; this.height = w; }
setHeight(h: number) { this.width = h; this.height = h; }
}A caller holding a Rectangle reasonably assumes setWidth(5); setHeight(4) yields an area of 20. Hand it a Square and it gets 16. No compiler error, no exception — just a silently violated expectation. The mutable Square is not a behavioral subtype of the mutable Rectangle, whatever geometry says. (Make both immutable and the problem dissolves, which shows the issue lives in the contract, not the taxonomy.)
Composition over inheritance — the reasoning, not the slogan
"Prefer composition over inheritance" is a heuristic, not a rule, and it has specific reasoning behind it. Inheritance is the tightest coupling available: the child depends on its parent's implementation details, inherits its entire public surface, and is fixed at compile time along exactly one axis of variation. Composition couples you only to an interface, lets you swap parts at runtime, and lets independent concerns vary independently — a BookingNotifier that composes a MessageChannel handles email, SMS, and push without a subclass explosion. As covered in objects and classes, objects collaborate more flexibly than they specialize.
Reach for it
- The subtype genuinely satisfies every contract of the supertype
- Callers should treat all subtypes uniformly through the parent abstraction
- Shared attributes and associations belong to the concept, not just to the code
Avoid it
- You only want to reuse implementation and the is-a claim feels forced
- The subtype must forbid or weaken anything the supertype allows
- Variation happens along several independent dimensions at once
None of this makes generalization bad. A Payment hierarchy with CardPayment and BankTransfer subtypes, each honoring the same authorize contract, is exactly what the triangle is for.
Practice
Model a library's lendable items. Start with Book, DvD, and ReferenceBook — reference books can never be borrowed. Draw a class diagram with an abstract LibraryItem supertype that has a borrow() operation and an association to Loan. Now decide: is ReferenceBook a valid subtype of LibraryItem, or does it violate substitutability for any caller that invokes borrow()? Restructure the hierarchy (hint: consider a Borrowable interface realized only by some classes, or splitting the supertype) so that every generalization arrow you keep passes the substitutability test. Write one sentence per arrow justifying the claim it makes.