Skip to content
Veltoryx Technologies
FreeLesson 1.4Beginner10 min readUpdated 31 October 2026

Class Diagrams and Relationships

Learn to read and draw class diagrams: what goes in a class box, the six kinds of relationships, how many objects each line connects, and how a diagram turns into Java code.

A class diagram is a map of your design. Just as a city map shows buildings and the roads between them, a class diagram shows classes and the connections between them. In an interview it is the picture that lets the interviewer understand your whole design in ten seconds.

The good news: you do not need perfect UML, which is the formal drawing standard. Interviewers want clear boxes and clear lines that you can explain. In this lesson you will learn exactly the pieces you need: the class box, six kinds of lines, the numbers on the lines, and a second kind of diagram, the sequence diagram, that shows how objects talk over time.

01The anatomy of a class box

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

  1. Name at the top, for example Loan.
  2. Fields, the data the class holds, in the middle.
  3. Methods, the things the class can do, at the bottom.

Each field and method has a small symbol in front that says who may use it:

SymbolMeaningJava word
+Visible to everyonepublic
-Visible only inside the classprivate
#Visible to subclassesprotected
~Visible inside the same package(no word)

A small example for a Loan class looks like this:

Loan
- dueDate: LocalDate
- returnedOn: LocalDate
+ isOverdue(): boolean
+ close()

The pattern for a field is name: Type. The pattern for a method is name(parameters): ReturnType. In an interview you can leave out the types when they are obvious. You can also leave out simple getters and setters, because they add noise without adding understanding.

An interface is drawn the same way, with the word <<interface>> above the name. An abstract class has its name written in italics, or <<abstract>> above it.

02The six relationships

Lines between boxes show how classes are related. There are six kinds. At first they look like a lot, but each one answers a simple question in plain English.

The six relationships in a class diagram, with their notation, meaning and Java equivalent
Fig. 1 · The six relationships in a class diagram, with their notation, meaning and Java equivalent

Let us take them one at a time, with a real-life picture for each.

Association: "knows about"

An association means one class keeps a long-term link to another. A Loan knows which Book it is for. In Java, an association is a field.

Aggregation: "has a, but the parts live on their own"

A Team has Player objects. But if the team is closed, the players still exist and can join another team. The diamond is empty (hollow) and sits at the "whole" end. In Java it is also a field, usually a list, and the players are created somewhere else and handed to the team.

Composition: "owns, and the parts end with the whole"

An Order has OrderLine objects. A line only makes sense inside its order. If the order is deleted, its lines go with it. The diamond is filled and sits at the "whole" end. In Java, the whole usually creates the parts and does not share them.

Inheritance: "is a kind of"

A Car is a kind of Vehicle. The line is solid and ends with a hollow triangle pointing at the parent. In Java, this is class Car extends Vehicle. Use it only for a true "is a" relationship, and remember the advice you will meet in the next module: when in doubt, prefer composition.

Implementation: "promises to do what an interface says"

EmailNotifier promises to do what Notifier says. The line is dashed with a hollow triangle pointing at the interface. In Java, this is class EmailNotifier implements Notifier.

Dependency: "uses briefly"

A ReceiptPrinter receives an Order as a method parameter, prints it, and forgets it. It does not keep a field. This is the weakest link: a dashed arrow pointing at the thing being used.

A quick guide for choosing the line

When you are unsure which line to draw, ask these questions in order:

QuestionIf yes
Is it an "is a" relationship?Inheritance (class to class) or implementation (class to interface)
Does one class keep the other as a field?An association; go to the next question
If the whole is deleted, do the parts disappear too?Composition. If the parts live on, aggregation
Is the other class only used inside one method?Dependency

In an interview, if you are unsure between aggregation and composition, a plain association line with a short word on it ("has", "owns") is fine, as long as you explain it.

03Multiplicity: how many objects?

The small numbers at the ends of a line tell you how many objects are on each side. This is called multiplicity.

NotationMeaningExample
1Exactly oneA Loan is for exactly one Book
0..1Zero or oneA Book has at most one current Loan
* or 0..*Zero or manyA Member can have zero or many Loan objects
1..*One or manyAn Order has at least one OrderLine

Read it like a sentence: "one Member has zero or many Loans", and "each Loan is for exactly one Book". In the library diagram from the previous lesson, the 1 and * on the lines say exactly this.

04From diagram to Java code

A good diagram almost writes the code for you. Here are the relationships above in Java, with only the lines that matter:

Relationships as Java code (key lines only)
// Implementation: EmailNotifier promises what Notifier says
public interface Notifier { void send(String to, String message); }
public class EmailNotifier implements Notifier { /* ... */ }

// Inheritance: a Car is a kind of Vehicle
public class Car extends Vehicle { /* ... */ }

// Association with multiplicity: one Member has many Loans, each Loan has one Book
public class Member {
    private final List<Loan> loans = new ArrayList<>();
}
public class Loan {
    private final Book book;
    private final Member member;
}

// Composition: the Order creates and owns its lines
public class Order {
    private final List<OrderLine> lines = new ArrayList<>();

    public void addLine(MenuItem item, int quantity) {
        lines.add(new OrderLine(item, quantity));
    }
}

// Aggregation: players are created elsewhere and handed to the team
public class Team {
    private final List<Player> players = new ArrayList<>();

    public void add(Player player) { players.add(player); }
}

// Dependency: Order is only used inside one method
public class ReceiptPrinter {
    public void print(Order order) { /* print and forget */ }
}

Look at the difference between Order and Team. Order writes new OrderLine(...) inside itself: it owns its lines. Team receives ready-made Player objects through add: the players have a life of their own. That one detail, who creates the parts, is usually how you tell composition from aggregation in code.

05Sequence diagrams: objects talking over time

A class diagram shows structure: which classes exist and how they are connected. It does not show what happens when something runs. For that you use a sequence diagram. It shows the conversation between objects, from top to bottom in time order.

Here is the borrow flow from our library example:

A sequence diagram of a member borrowing a book through LibraryService, Book and Loan
Fig. 2 · A sequence diagram of a member borrowing a book through LibraryService, Book and Loan

How to read it:

  • Each box at the top is an object (a participant). The dashed vertical line under it is its lifeline: it exists while time passes.
  • A solid arrow is a call: one object asks another to do something. The words on the arrow are the method name.
  • A dashed arrow going back is the answer (the return value).
  • The thin tall rectangle on LibraryService shows the time when it is busy handling the call.
  • Time runs from top to bottom, so the order of the arrows is the order of the events.

Now read the diagram as a story. The member asks LibraryService to borrow a book. The service asks the book whether it is available, and the book answers true. The service creates a Loan, tells the book it is now borrowed, and gives the member back the loan with its due date.

You can now see that the sequence diagram and the class diagram support each other. Every arrow in the sequence diagram must correspond to a method that exists on a class in the class diagram. If you draw an arrow for a method that no class has, you have found a gap in your design, which is exactly the purpose of the walk-through step from the previous lesson.

Class diagramSequence diagram
ShowsWhich classes exist and how they connectWhat happens, in order, in one scenario
TimeNo time, a snapshotTime flows from top to bottom
Use it toExplain the structure of the designProve that the design can do a task
Draw itFirstAfter the class diagram, for the main scenarios

06Common mistakes

  • Drawing every getter and setter. The diagram becomes a wall of text. Show only what explains the design.
  • Arrows in the wrong direction. An arrow normally points from the class that knows to the class being known, and the triangle for inheritance points at the parent.
  • Using inheritance for everything. Many relationships that look like "is a" are really "has a" or "plays a role". If you are not sure, use a field.
  • Mixing up aggregation and composition. Ask the deletion question: if the whole disappears, do the parts disappear too?
  • Too many boxes. Aim for five to eight core classes on one diagram. More than that, and you are probably showing details that belong in a second diagram.
  • Diagram and code that disagree. If your code has a method that the diagram does not, update one of them before you leave the board.

07In the interview

  • When do you choose composition over inheritance? Whenever the relationship is "has a" or "uses the behaviour of", not "is a kind of". Composition keeps classes independent and lets you change behaviour at runtime. This idea is covered in the next module.
  • Do I need exact UML in an interview? No. Clear boxes, correct relationship lines, and a short explanation are enough. If you forget a symbol, label the line in words.
  • Which diagram should I draw first? The class diagram, because it explains the structure. Then draw a sequence diagram for the one or two most important scenarios, to prove that the design works.

08Check your understanding

Quiz · 4 questions

0 of 4 answered

  1. 1. A Team holds a list of Player objects, but a Player can still exist after the team is dissolved. Which relationship is this?

  2. 2. What does the symbol - in front of a field in a class box mean?

  3. 3. In a sequence diagram, what does a dashed arrow pointing back to the caller show?

  4. 4. Which line in a class diagram best shows that EmailNotifier follows the Notifier interface?