Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 50 ยท Modeling with UML

Class Diagrams

Learn how to read and draw UML class diagrams: attributes, operations, visibility, association, aggregation, composition, inheritance and multiplicity.

The class diagram is the workhorse of UML. It shows the types in a system, the data and behavior each one has, and how they are related. When people say "draw the design", they very often mean a class diagram. It is a structural diagram: it describes what exists, not what happens over time (for that, see sequence and activity diagrams).

Class diagrams fit object-oriented languages naturally, but the idea of "a thing with data and operations, related to other things" helps even when you write in another style. UML itself cannot run; the runnable examples below are JavaScript stand-ins that store a class model as plain data, generate code skeletons from it, and write it back out as Mermaid text. They show how modeling tools work in principle; they are not real UML tools, and the code they print is a starting skeleton, not a finished design.

Anatomy of a class

A class is drawn as a rectangle with three compartments, from top to bottom:

  1. The name, in bold. Abstract classes are in italics or marked <<abstract>>.
  2. The attributes (data the class holds), such as -title: String.
  3. The operations (things it can do), such as +borrow(member: Member): boolean.

Each member starts with a visibility marker:

Marker Meaning Who can use it
+ public any other class
- private only this class
# protected this class and its subclasses
~ package classes in the same package

Most models mark attributes private and operations public, in line with information hiding. You do not have to list everything; a diagram for a discussion may show only names, or only the members that matter for the point being made.

Relationships

Classes are linked by lines whose ends tell you the kind of relationship.

Relationships between classes

Association One class knows about another.
Aggregation A whole with parts that live on their own.
Composition Parts that die with the whole.
Inheritance A subclass is a kind of superclass.

Hollow diamond = aggregation. Filled diamond = composition. Hollow triangle = inheritance.

1 / 2

Association is a plain line, optionally with a name (places), a direction arrow, and a multiplicity at each end. "A customer places many orders" is Customer 1 --> * Order. Role names at the ends can clarify (employer and employee).

Aggregation is "has a" in the loose sense: a library has books, but a book can exist without that library. It is drawn with a hollow diamond at the whole. Many modelers skip aggregation because it adds little over plain association; it is fine to leave it out.

Composition is stronger: the parts cannot outlive the whole. An order line has no meaning without its order; delete the order and its lines go too. It is drawn with a filled diamond at the whole, and the multiplicity at the whole end is normally exactly one.

Inheritance (generalization) means a subclass is a kind of its superclass and gets its attributes and operations. It is drawn as a line with a hollow triangle at the superclass. A related idea is realization: a class implements an interface, drawn as a dashed line with a hollow triangle. Use inheritance only for true "is a" relationships; for reuse, prefer composition.

Dependency (a dashed arrow) means a class uses another briefly, for example as a method parameter, without holding it as data.

A worked example

classDiagram
    class LibraryItem {
        <<abstract>>
        -String title
        +isAvailable() boolean
    }
    class Book {
        -String isbn
    }
    class Dvd {
        -int minutes
    }
    class Member {
        -String name
        +borrow(item LibraryItem) boolean
    }
    class Loan {
        -Date dueDate
        +isOverdue() boolean
    }
    class Library {
        +String name
    }
    LibraryItem <|-- Book
    LibraryItem <|-- Dvd
    Library "1" o-- "*" LibraryItem : holds
    Member "1" --> "*" Loan : has
    Loan "*" --> "1" LibraryItem : for

Books and DVDs are kinds of library items. A library holds many items (aggregation); a member has many loans, and each loan refers to one item.

Reading it: Book and Dvd inherit title and isAvailable() from LibraryItem, and add their own data. The Loan class carries information that belongs to neither member nor item but to the act of borrowing; this "association class" pattern is common and often appears when a many-to-many link has attributes of its own.

From requirements to classes

A practical way to find classes is to read your requirements or use cases and underline the nouns: member, book, loan, library. Nouns become candidate classes or attributes; verbs become operations or relationships. Then prune: drop synonyms, vague nouns ("system"), and things that are just attributes (a book's title is an attribute, not a class). Check each candidate class has a clear responsibility. Finally, look at relationships and ask for multiplicities: can a loan exist without an item? Can an item be on several loans at once?

Class model to code skeleton

Many tools generate code skeletons from class diagrams and the reverse. The idea is straightforward enough to model yourself. The stand-in below holds a class model as data and prints a JavaScript skeleton: attributes become private fields (JavaScript's # marks private, just like UML's -), a subclass uses extends, a composition creates its part inside the constructor, and a relationship with a * multiplicity becomes an array.

JS Generate class skeletons from a model

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

No output.

The generator only knows the model, so what it prints is deliberately empty: constructors with placeholders, and operations that throw. The real design work is filling in behavior and deciding things the diagram never said, such as validation rules. The generated code also has gaps you must close yourself: the super(/* parent data */) line in Book is only a placeholder, and Member has a #loans array but nothing yet that adds to it. Treat generation as a typing-saver, not as design.

Model back to a diagram

The reverse direction is just as useful: if your model is data, you can print a diagram from it. This stand-in turns a model like the one above into Mermaid text, including multiplicities. Paste the printed text into any Mermaid renderer to draw it.

JS Print a model as Mermaid text

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

No output.

If you paste that text into a Mermaid renderer you get a diagram of customer, order and order line; the *-- arrow is what draws the filled diamond of composition. Keeping the model as text or data means diagrams can be stored with the code and regenerated, instead of drifting out of date in a drawing tool.

Tips for useful class diagrams

  • Scope each diagram. Show one subsystem or one scenario, not the whole codebase.
  • Choose the level of detail deliberately. An analysis diagram (domain concepts) shows no operations; a design diagram near the code shows types and visibility.
  • Name relationships and give multiplicities. An unlabeled, unqualified line leaves the reader guessing.
  • Favor composition over deep inheritance trees. Diagrams with five levels of inheritance often point to a design problem.
  • Do not mirror every getter and setter. They add noise.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the marker '-' in front of a UML attribute mean?

    '-' is private, '+' is public, '#' is protected and '~' is package visibility.
  2. 2. Which relationship says the parts cannot exist without the whole?

    Composition (filled diamond) means the parts live and die with the whole, like order lines with their order.
  3. 3. What does the multiplicity '0..1' mean?

    '0..1' means the link is optional, with at most one object on that end.
  4. 4. A Book 'is a kind of' LibraryItem. Which relationship models this?

    Inheritance, drawn with a hollow triangle at the superclass, expresses an 'is a' relationship.
  5. 5. Why is generated code from a class model only a skeleton?

    A class diagram shows structure, not what operations do. Behavior and rules still have to be designed and written.

Summary

  • A class box shows a name, attributes and operations, each with a visibility marker (+, -, #, ~).
  • Association links classes; aggregation and composition add whole-part meaning (composition means the parts die with the whole); inheritance expresses "is a".
  • Multiplicities such as 1, 0..1, * and 1..* say how many objects take part on each end.
  • Finding nouns in requirements is a practical way to get candidate classes; prune and check responsibilities afterward.
  • Models held as data can generate skeleton code or diagram text, but behavior and rules still need design.

Next: Sequence and Activity Diagrams.