Modern AI Engineering

UML Class Diagram and Sequence Diagram Explained with Examples

By Modern AI Engineering · · 7 min read

UML stands for Unified Modeling Language. It is a standard set of shapes and arrows for drawing a software design, so that any engineer can read the drawing the same way. Two UML diagrams do most of the work in design discussions and interviews: the class diagram and the sequence diagram.

This article explains how to read both, what each arrow means, and how to draw them for a small example: a food-ordering app.

What is a class diagram?

A class diagram shows the structure of the code. Each class is a box with three parts: the name at the top, the attributes (the data it stores) in the middle, and the methods (what it can do) at the bottom.

A symbol in front of each attribute or method shows who may use it. A plus sign means public: any class can use it. A minus sign means private: only the class itself. A hash sign means protected: the class and its subclasses.

The six relationships in a class diagram

The lines between the boxes carry most of the meaning. There are six, and each has its own arrow.

RelationshipHow it is drawnRead it asExample
InheritanceSolid line, hollow triangle at the parentis aA Car is a Vehicle
ImplementationDashed line, hollow triangle at the interfacecan act asCardPayment implements PaymentMethod
CompositionSolid line, filled diamond at the ownerownsAn Order owns its OrderItems
AggregationSolid line, hollow diamond at the holderhasA Garage has Vehicles
AssociationSolid line, open arrowknowsAn Order knows its Customer
DependencyDashed line, open arrowuses for a momentOrder.checkout uses a PaymentMethod

Aggregation vs composition

These two are confused more than any others. Ask one question: if the whole is deleted, should the part be deleted too? If yes, it is composition. Delete an order and its order items mean nothing. If no, it is aggregation. Close a garage and the vehicles still exist.

In code, composition usually means the owner creates the part itself and nobody else holds it. Aggregation means the part is created elsewhere and passed in.

How to draw a class diagram, step by step

  1. Write the requirements as short sentences.
  2. Underline the nouns. They are your candidate classes.
  3. Underline the verbs. They are your candidate methods. Put each method on the class that owns the data it needs.
  4. Give each class only the attributes this design needs.
  5. Connect the classes: for each pair, decide whether it is "is a", "owns", "has", "knows" or "uses for a moment".
  6. Put anything that comes in several kinds, such as payment methods, behind an interface.

What is a sequence diagram?

A class diagram cannot show order in time. A sequence diagram does. It follows one use case from start to finish. Each participant is a box at the top with a dashed line, called a lifeline, running down from it. Time runs downwards. A solid arrow is a call from one participant to another. A dashed arrow is the reply.

For the food-ordering app, the sequence for placing an order reads: the customer app calls placeOrder on the order service. The order service works out the total. It asks the payment method to pay. The payment method replies that it is paid. Only then does the order service tell the restaurant. Finally it confirms the order to the app.

A sequence diagram is also a test of the class diagram. Every arrow must be a method that exists on the class receiving it. If an arrow has nowhere to land, the class diagram is missing something.

Class diagram vs sequence diagram

Class diagramSequence diagram
ShowsStructureBehaviour over time
Boxes areClassesObjects or services taking part
Lines areRelationshipsMethod calls and replies
How many to drawOne per designOne per main use case

Build these diagrams interactively

Reading about arrows only goes so far. In the AI Engineering Bootcamp lesson on system design, you build the food-order class diagram and its sequence diagram one step at a time, click any box to see why it exists, and then run the Python code that the diagram turns into.

Frequently asked questions

What is a UML class diagram?

A drawing of the classes in a design, with their attributes and methods, and the relationships between them.

What is the difference between aggregation and composition?

In composition the part cannot exist without the whole; it is drawn with a filled diamond. In aggregation the part can exist without the whole; it is drawn with a hollow diamond.

What is the difference between a class diagram and a sequence diagram?

A class diagram shows structure: classes and relationships. A sequence diagram shows the order of calls between objects for one use case.

Which UML diagrams do I need for interviews?

The class diagram and the sequence diagram cover almost every low-level design interview. A state diagram helps for questions such as a vending machine.

Which way does the inheritance arrow point?

From the child to the parent. The hollow triangle touches the parent class.

Learn it properly: the AI Engineering Bootcamp

More articles