Skip to content
flowchart.tools

UML

UML class diagram relationships: association, aggregation, composition and the rest

By Published 6 min read

The short answer

UML class diagrams use six relationships. Association (a solid line) means one class holds a reference to another. Aggregation (hollow diamond) and composition (filled diamond) are associations with a whole and parts; with composition the parts cannot outlive or be shared by the whole. Generalization (solid line, hollow triangle) is inheritance, realization (dashed line, hollow triangle) is implementing an interface, and dependency (dashed line, open arrow) means one class uses another without keeping it. Multiplicities such as 1, 0..1 and * at each end say how many objects take part.

A UML class diagram is mostly boxes, and the boxes are the easy part. The meaning lives in the lines between them: whether one class owns another, merely knows about it, inherits from it or only uses it for a moment. UML gives each of those a distinct line, and a reader who knows the six can learn a codebase's structure from one diagram.

This guide covers each relationship with the line it uses, what it means in code, and the mistakes that make a class diagram say something the code does not.

The six UML class diagram relationshipsAssociation: Order to Customer, solid line with an open arrow. Aggregation: Team with a hollow diamond to Employee. Composition: Order with a filled diamond to LineItem. Generalization: CardPayment to Payment, solid line with a hollow triangle. Realization: StripeGateway to the PaymentGateway interface, dashed line with a hollow triangle. Dependency: OrderService to TaxCalculator, dashed line with an open arrow.ASSOCIATIONholds a referenceOrderCustomerAGGREGATIONparts live on their ownTeamEmployeeCOMPOSITIONwhole owns the partsOrderLineItemGENERALIZATIONis a (inheritance)CardPaymentPaymentREALIZATIONimplementsStripeGateway«interface»PaymentGatewayDEPENDENCYuses, does not keepOrderServiceTaxCalculator
Six relationships, three line ends and two line styles. Diamonds sit on the whole; triangles point at the parent or interface.

The six relationships at a glance

Relationship Line End marker Means In code
Association Solid None, or an open arrow for navigability One class holds a reference to another A field: customer: Customer
Aggregation Solid Hollow diamond on the whole A whole that groups parts which live independently A collection of objects created elsewhere
Composition Solid Filled diamond on the whole A whole that owns its parts' lifecycle Parts created and discarded by the whole
Generalization Solid Hollow triangle on the parent Inheritance: "is a" class CardPayment extends Payment
Realization Dashed Hollow triangle on the interface Implements an interface class StripeGateway implements PaymentGateway
Dependency Dashed Open arrow on the supplier Uses temporarily, without keeping a reference A parameter, local variable or static call

Two line styles and three markers carry all of it. A useful mnemonic: solid lines are structure that persists; dashed lines are weaker (an interface contract, or a passing use). Diamonds sit on the owner; triangles point at the more general type.

Association: "has a reference to"

A solid line means objects of one class hold references to objects of the other for longer than a single method call. Add an open arrowhead to show navigability, which side can reach the other: Order → Customer means an order knows its customer, but the customer object does not hold its orders.

Associations carry multiplicity at each end, written as a number or range:

Multiplicity Meaning
1 Exactly one
0..1 Zero or one (optional)
* or 0..* Any number, including none
1..* At least one
2..4 A specific range

Multiplicity is read like crow's foot cardinality: stand at one class and read the number at the other end. Customer 1 —— 0..* Order says each customer has zero or more orders, and each order has exactly one customer. The crow's foot guide explains the same idea for database tables.

You can also name the end with a role: Order —— Customer with the role buyer at the customer end tells the reader what the field is called and why it exists.

Composition: the whole owns the parts

A filled diamond on the whole's end says three things at once:

  1. A part belongs to at most one whole at a time.
  2. The whole is responsible for the part's lifecycle: when the whole is deleted, its parts go with it.
  3. The multiplicity at the whole's end is therefore 1 (or 0..1), never *.

Order ◆—— 1..* LineItem is the textbook case. A line item does not exist outside its order, cannot belong to two orders, and is deleted with the order. In code, the order typically creates its items and never hands them out to be stored elsewhere:

class Order {
  private readonly items: LineItem[] = [];

  addItem(product: Product, quantity: number) {
    this.items.push(new LineItem(product, quantity)); // created by the whole
  }
}

Aggregation: the honest answer

A hollow diamond marks a whole made of parts that exist on their own and may be shared: a Team aggregates Employees, and deleting the team does not delete the people.

The honest expert answer is that aggregation adds little. The UML 2.5 specification itself says the precise semantics of shared aggregation "varies by application area and modeler". In practice, a hollow diamond and a plain association with multiplicity communicate the same thing, and readers interpret the diamond inconsistently. A good default:

  • Draw composition when lifecycle ownership matters, because it changes how the code must behave.
  • Draw association for everything else.
  • Draw aggregation only if your team has agreed what it means, and say so in a note on the diagram.

Generalization: inheritance

A solid line with a hollow triangle pointing at the parent: CardPayment ——▷ Payment. Read it as "is a": a card payment is a payment. Abstract classes are shown with an italic name or the «abstract» keyword.

The triangle always touches the more general class. Several subclasses can share one triangle with their lines merged into a tree, which is easier to read than separate arrows.

Realization: implementing an interface

Same triangle, dashed line: StripeGateway - - -▷ PaymentGateway. The target is an interface, marked with «interface» above its name. Realization is about a promise (this class provides these operations), while generalization is about inheriting structure and behavior.

Dependency: uses, but does not keep

A dashed line with an open arrow from the class that uses to the class that is used: OrderService - - -> TaxCalculator. It means a change to the supplier may require a change to the client, without the client holding a reference. Typical causes are a method parameter, a local variable, a static call or a created-and-returned object.

Dependencies are everywhere in real code, so draw only the ones that matter to the diagram's purpose: the dependency on an external service, for example, or one that crosses a layer boundary and should not.

Common mistakes

The diamond on the wrong end. It sits on the whole, not the part. Order ◆—— LineItem, never Order ——◆ LineItem. The diamond is a marker of the owner, not an arrowhead.

Inheritance arrows pointing down. The triangle points from subclass to superclass. If your arrows point from Payment to CardPayment, the diagram says the opposite of the code.

Composition with shared parts. If a Product appears in many orders, it cannot be a composed part of Order. That relationship is an association from LineItem to Product.

Every field drawn as a line. Attributes of simple types (String, Money, Date) belong in the class box. Draw a line when the type is another class on the diagram.

Dependencies on everything. A diagram with a dashed arrow for every import is unreadable. Show the dependencies that answer the question the diagram exists for.

Drawing it in flowchart.tools

The editor's UML shape set has class boxes with name, attribute and operation compartments, interfaces, actors, use cases, states, components and packages. Connector presets set the line style and markers for you:

  • Composition and Aggregation put the diamond at the start of the line, so draw from the whole to the part.
  • Inheritance and Realization put the triangle at the end, so draw from the subclass or implementation to the parent or interface.
  • Dependency puts the open arrow at the end, so draw from the client to the supplier.
  • Association is a solid line with an open arrow for navigability; select a line and set its ends to none for an undirected association, and add the multiplicities as the connector label.

The online store class diagram uses the same classes as this article: Customer, Order, composed LineItems, Product, and an abstract Payment with two subclasses. Open it in the editor to see each relationship drawn. For diagrams kept in a repository, Mermaid's classDiagram syntax renders in the Mermaid editor, where *-- is composition, o-- aggregation, <|-- inheritance, ..|> realization and ..> dependency.

Common questions

What is the difference between aggregation and composition?
Ownership of the parts' lifecycle. In composition the whole creates and destroys its parts, and a part belongs to exactly one whole at a time: deleting an order deletes its line items. In aggregation the parts exist independently and may be shared: deleting a team does not delete its members.
Which end does the diamond go on?
On the whole. In Order ◆— LineItem, the filled diamond touches Order. A common mistake is putting it on the part because the arrow seems to "point" there; the diamond is a marker of the owner, not an arrowhead.
Which way does the inheritance arrow point?
From the subclass to the superclass, with the hollow triangle touching the superclass. Read it as "is a": CardPayment is a Payment.
Should I draw aggregation at all?
Often a plain association says the same thing. The UML specification itself notes that the precise semantics of shared aggregation vary by application area and modeler. Draw composition when lifecycle ownership matters, and use association for everything else unless your team has agreed what aggregation means.

Put this into practice

Open the editor and draw it. Connectors stay attached, the layout tidies itself, and your diagram never leaves your browser.

Open mermaid editor

Related diagram types and tools

Templates to start from

More guides