Learn

Architecture & pattern guides

Read the idea, build it on a Grid, prove it in a challenge. Short, practical guides — illustrated with RodGrid's own nodes — on system design, design patterns, flow modeling, and how to get the most out of the app.

All articles

Inheritance, interfaces and abstract types

How to draw and read generalization and realization in a class diagram, how abstract classes and interfaces are marked, and what each arrow claims about the code.

Same triangle, two different lines

Two of the lines in a class diagram end in the same hollow triangle, and a reader who glances at the arrowhead alone will mix them up. What separates them is the line itself, solid or dashed, and that one difference carries two different claims about the code.

Generalization — a hollow triangle on a solid line — says "is a kind of". Realization — a hollow triangle on a dashed line — says "promises to satisfy this contract". A concrete class extends an abstract one by generalization; it implements an interface by realization. Both triangles point at the more abstract end.

One contract, kept two ways

Dashed lines with a triangle are realizations, solid ones are generalizations, and every triangle points at the more abstract type.

Read it from the bottom. CsvExporter and PdfExporter hang from FileExporter on solid lines: each is a file exporter, inherits its path and its export(), which opens the file, writes the header and calls formatRows, and supplies the one operation the parent leaves open. A base class that fixes the steps and leaves one to its subclasses is the shape Template Method describes. FileExporter hangs from Exporter on a dashed line: it promises to provide export(report). So does WebhookExporter, which writes no file and has nothing to share with the others, so it keeps the same promise on its own.

Now read the diagram for what it does not contain: no line from ReportScheduler to any concrete exporter. That absence is the design. Adding an exporter for spreadsheets touches neither ReportScheduler nor any existing exporter; only the code that decides which exporters to create learns about the new one — and that is what the interface buys. The dashed open arrow it does contain, from ReportScheduler to the interface, is a plain dependency; Association, aggregation, composition explains it alongside the other lines between boxes.

Marking what is abstract

The «interface» keyword above a name marks a contract. An abstract class is marked, in UML's own notation, by writing its name in italics or by adding {abstract} beside it, and the same marks apply to a single operation, as on formatRows above. Italics do not survive a whiteboard or a quick sketch, so many teams write «abstract» above the name instead, as this diagram does. Whichever you pick, use it the same way throughout a diagram. The # before path marks it protected, visible to subclasses but not to other classes; the other visibility marks are in The class box: name, attributes, operations.

NoteAbstract class or interface? An abstract class is for shared implementation and state plus a hole for subclasses to fill; an interface is for a contract, and while Java and C# now let it carry default methods, it holds no instance state. If you find yourself with an abstract class whose methods are all abstract and that holds no state, you probably wanted an interface. The diagram above uses both for that reason: FileExporter has code to share, and Exporter is the promise that WebhookExporter can keep without it.

Drawing it yourself

When you draw one yourself, decide in this order:

  1. The parent's box: «interface» if it is a contract with no state of its own, an abstract class if it has code to share but should never be created on its own, a plain class otherwise.
  2. The line: Solid line + hollow triangle = extends, dashed line + hollow triangle = implements. An interface that extends another interface takes the solid line, because that is generalization too.
  3. The direction: from child to parent, with the triangle at the parent. Placing parents above their children, as this diagram does, makes the finished diagram read upward from the specific to the general, the way you just read this one.

Inside the child's box, list only what it adds or overrides. CsvExporter shows formatRows because it supplies it; repeating path and export() from the parent makes the reader compare boxes line by line to find what differs. The same goes for a second dashed line from CsvExporter to Exporter: it already keeps that promise through its parent.

UML lets a class have more than one generalization. Many languages do not: Java and C# allow one superclass and any number of interfaces, so in a diagram meant for those languages, two solid lines with triangles leaving one class (not an interface) describe a design the code cannot express.

NoteOn a Challenge Grid the hollow triangle and the dashed line cannot be drawn — every connection gets the default line and arrow. Label it instead, using this convention: «extends» for generalization, «realize» for realization, «use» for a dependency, drawn from the concrete type toward the abstraction as above.

Diagrams that say something else

  • Inheriting to reuse code rather than to substitute types. If a Stack "is a" ArrayList only because it wants push and pop cheaply, it now also exposes insert-at-index — composition would have kept that hidden. A solid line promises the child can stand in wherever the parent is expected; that rule, Liskov substitution, gets its full treatment in SOLID design principles.
  • Deep hierarchies. A hierarchy several levels deep is often a sign the varying thing wants to be a field or a strategy object (Strategy), not a subclass.
  • An abstract class with no abstract members and no reason to forbid instantiation — if every member has a default and the base is a sensible object on its own, abstract is decorative.

Extends and realize, labelled

Payment Strategies is the Challenge built on generalization, with room for realization too. Draw the hierarchy its brief describes: an abstract type for what every payment method shares, at least three concrete methods that each supply their own authorization step, and an interface only if you find a contract worth naming. Label each generalization «extends» and each realization «realize», and decide where refund() lives so it is written once. Then read the diagram aloud from the bottom: every «extends» line should make a true "is a kind of" sentence. Notification Channels drills realization on its own, and Chess Pieces puts an abstract type inside a larger model.

Practice this