Card 02/ 08

ConceptDifficulty: Beginner2 min

UML Relationships and Their Arrows

Inherits, implements, has, owns, knows and uses: six relationships, each drawn differently.

A UML class diagram draws six kinds of relationship between classes, and each one has its own combination of line and arrowhead. The line is solid or dashed, and the end carries a triangle, a diamond or an open arrow. This card names all six, says what each means, and gives the code each one usually becomes.

The diagram shows each relationship once, between a pair of classes chosen so the meaning is easy to say aloud.

The six relationships between classes
Each relationship, how it is drawn, and the code it implies
RelationshipDrawn asRead it asUsually in code
InheritanceSolid line, hollow triangle at the parentA dog is an animal.class Dog(Animal) or extends
RealisationDashed line, hollow triangle at the interfaceA bird implements flying.implements Flyer, or a class that matches a protocol
AssociationSolid line, open arrow at the class it knowsA teacher knows their courses.A field holding the other object
AggregationSolid line, hollow diamond at the wholeA team has players, who can leave it.A field holding parts passed in from outside
CompositionSolid line, filled diamond at the wholeA house owns its rooms, which go when it does.A field holding parts the whole creates itself
DependencyDashed line, open arrow at the class it usesA report uses a printer for a moment.A parameter or a local variable

Two rules sort most of the table. A triangle always means "is a kind of": solid for a class, dashed for an interface. A diamond always sits on the whole, never the part: hollow when the part has a life of its own, filled when the whole creates and destroys it.

The remaining two are about how long one class holds the other. Association is a lasting link, kept in a field. Dependency is a passing one, an object received as a parameter or created inside one method and forgotten afterwards, which is why its line is dashed.

Most pattern diagrams use only four of the six: inheritance, realisation, association and dependency. Aggregation and composition appear where a pattern builds a whole from parts, as Composite and Builder do.