Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 39 ยท Class and Object Diagrams

Associations and Multiplicity

Learn how to draw associations between UML classes: names, role names, multiplicity, navigability, association classes, reflexive and qualified associations.

Classes alone say little. The real structure of a system lies in how classes are connected. An association is a lasting structural link: instances of one class know about, or hold, instances of another. It is drawn as a solid line, and it is the backbone of most class diagrams.

A basic association

classDiagram
    class Customer {
        +String name
    }
    class Order {
        +Date placedOn
    }
    Customer "1" --> "0..*" Order : places

Read it as: one Customer places zero or more Orders.

Three decorations turn a plain line into a precise statement:

  • A name (places) with a reading direction.
  • Multiplicity at each end (1, 0..*).
  • Navigability: the arrowhead says who can find whom.

Multiplicity in practice

Multiplicity answers "how many?" at both ends, and the two answers are independent.

Customer end Order end Reading
1 0..* A customer has any number of orders; each order has exactly one customer
1 1..* Every customer must have at least one order
0..1 0..* An order may exist without a customer (guest checkout)
* * Many-to-many (see below)

Read the multiplicity at the far end of the line as "how many of those does one of these have". Many beginners read it backwards, so practice by saying the sentence aloud.

Role names

A role name describes the part a class plays in the association, and it usually becomes the name of the field in code.

classDiagram
    class Person
    class Company
    Person "*" --> "1" Company : employer
    Person "1" --> "*" Person : manager

employer is the role of Company from a Person's point of view. The second line is a reflexive association: a Person can be a manager of other Persons.

Role names are most valuable when two classes are connected twice (a Flight has origin and destination, both Airport) or when the class name alone is vague.

An arrow shows navigability: which side can reach the other. Order --> Customer means an Order object holds a reference to its Customer; the Customer need not know its orders. A line with no arrowheads usually means "not decided yet" or bidirectional; a cross (X) can state non-navigability explicitly.

In design diagrams, prefer unidirectional associations. Two-way links must be kept in sync and tend to tangle code. Pick the direction you actually need.

Many-to-many and association classes

A student enrolls in many courses and a course has many students. Often the link itself carries data: when did the student enroll, and what grade did they receive? That data belongs to neither end. UML provides an association class: a class attached to the association with a dashed line.

classDiagram
    class Student {
        +String name
    }
    class Course {
        +String title
    }
    class Enrollment {
        +Date enrolledOn
        +String grade
    }
    Student "*" -- "*" Course : enrolls in
    Enrollment .. Student
    Enrollment .. Course

Mermaid has no true association-class symbol, so this draws Enrollment next to the link. In UML proper, Enrollment hangs from the Student-Course line by a dashed connector.

When you implement it, an association class usually becomes a real class with references to both ends, which is exactly how you would build a join table.

Qualified associations

A qualifier is a lookup key that narrows the far end. A Library finds a Book by isbn: the association with qualifier isbn gives multiplicity 0..1 instead of *. It maps to a dictionary or map in code. Qualifiers are rare in diagrams but precise when needed.

Associations vs attributes

An association and an attribute can describe the same fact. Choose by importance:

  • Use an attribute for simple values (String name, Date placedOn).
  • Use an association when the other end is a class in the model that you want to show on the diagram.

Never show both. If Order has an association to Customer, do not also list customer : Customer inside the Order box.

Python Which side holds the reference?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

If the design needs "all orders of a customer" often, add the other navigation direction (Customer --> Order) and a orders collection, and take care to keep both sides consistent.

Python A many-to-many link with its own data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Anatomy of an association

Name A verb phrase, read in one direction.
Multiplicity How many at each end.
Role The part played; becomes a field.
Navigability Who can reach whom.

Say it aloud as a sentence to check it.

1 / 2

Common mistakes

  • Backwards multiplicity. Always test with a sentence: "one Customer has 0..* Orders".
  • Too many bidirectional links. Choose a direction; add the other only when needed.
  • Missing the association class. Data like grade stuck on Student or Course is a design error.
  • Association instead of inheritance (or the reverse). An association is "has a relationship with"; inheritance is "is a kind of" (next lesson).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In Customer "1" --> "0..*" Order, what is true?

    The multiplicity near each class tells how many of that class take part from the other side's view.
  2. 2. What does the arrowhead on an association indicate?

    The arrow shows the direction in which one object can find the other.
  3. 3. A student's grade in a course belongs best on:

    Data about the link itself goes in an association class.
  4. 4. What is a role name for?

    Roles such as employer or manager clarify meaning and often become field names.

Summary

  • An association is a solid line between classes, with a name, role names and multiplicity.
  • Multiplicity at each end counts instances independently; read it as a sentence.
  • Arrowheads show navigability; prefer one direction.
  • Many-to-many links that carry data use an association class.
  • Qualifiers model lookups by key.
  • Do not draw the same fact as both an attribute and an association.

Next: Inheritance and Interfaces