Topic 8 of 9 · Labs IX–X
UML Class Diagrams
Classes, attributes, operations and visibility; association, multiplicity, aggregation, composition and dependency; generalization, abstract classes, interfaces and realization, with the telephone and ATM examples.
← → move between slides · F full screen · M slide list
Before we start
Why this matters
Your sequence diagrams are full of messages: verifyPIN(), withdraw(), debit(). Which class owns each one, and what does each class need to remember?
The class diagram is the blueprint programmers turn into code and database tables. It is where all the earlier models meet: nouns from the SRS become classes, messages from sequence diagrams become operations.
This topic covers both labs: the basic class diagram (Lab IX) and the extended one with inheritance, abstract classes and interfaces (Lab X).
Outline · Labs IX–X
Topics in this chapter
Select a topic to jump to the slide that explains it.
Concept 1 of 15
UML and the class diagram
UML (Unified Modeling Language) is the standard visual language for the architecture, design and implementation of software. Its diagrams fall into two families:
| Structural (static) | Behavioural (dynamic) |
|---|---|
| Class, object, component, deployment, package | use case, sequence, communication, activity, state machine |
A class diagram shows the classes of a system, their attributes and operations, and the relationships between them. It is the most used UML diagram and the only one that maps almost one-to-one onto code.
- Describes the static view of the system.
- Models how the elements of that view collaborate.
- Describes the functions the system performs through its operations.
- Is the blueprint for object-oriented code (Java, C#, Python) and for the database design.
- Supports forward engineering (diagram → code) and reverse engineering (code → diagram).
Class diagram basics
Anatomy of a class
Class diagram basics
Anatomy of a class (cont.)
- Name: centred, bold, starts with a capital letter, singular noun (
Account, notAccounts). - Attributes: left-aligned, first letter lowercase, one per line:
visibility name : type = default, e.g.- balance : double = 0. - Operations:
visibility name(parameter : type) : returnType, e.g.+ withdraw(amount : double) : boolean. - The attribute and operation compartments are optional; an empty box with only a name is fine in an overview diagram.
Class diagram basics
Visibility and other markers
| Symbol | Visibility | Who can access it |
|---|---|---|
+ | public | any class |
- | private | only this class |
# | protected | this class and its subclasses |
~ | package | classes in the same package |
| Marker | Meaning |
|---|---|
| underlined | static (class-level) member |
| italic name | abstract class or operation |
/ name | derived attribute (calculated, e.g. /age) |
{readOnly} | cannot change after creation |
«interface» | stereotype on an interface |
-) and accessed through public operations (+) — that is encapsulation, from your OOP course.Class diagram basics
Visibility and other markers (cont.)
Concept 4 of 15
Relationships at a glance
Concept 4 of 15
Relationships at a glance (cont.)
| Relationship | Notation |
|---|---|
| Association | solid line (open arrow if one-way) |
| Inheritance (generalization) | solid line, hollow triangle at the parent |
| Realization / implementation | dashed line, hollow triangle at the interface |
| Dependency | dashed line, open arrow |
| Aggregation | solid line, hollow diamond at the whole |
| Composition | solid line, filled diamond at the whole |
The lab manual’s Table X.1, in words.
Relationships
Association and multiplicity
- An association is a lasting structural link: objects of one class hold references to objects of the other.
- Bidirectional (plain line): both classes know each other. Unidirectional (open arrow): only the source knows the target.
- Label it with a name (a verb: owns) and optionally role names at the ends.
- Multiplicity at each end says how many objects take part:
1,0..1,*,1..*,2..4. - Read multiplicity from the far end: “one Company employs 1..* Employees; each Employee works for 1 Company.”
Relationships
Association and multiplicity (cont.)
Interactive
Reading multiplicity
Relationships
Aggregation, composition and dependency
| Aggregation ◇ | Composition ◆ | Dependency ⇢ | |
|---|---|---|---|
| Meaning | whole–part, “has a” | strong whole–part, “owns / is made of” | “uses” for a short time |
| Part’s lifetime | independent of the whole | ends with the whole | no lasting link |
| Can a part be shared? | yes | no — one whole only | — |
| Example | Department ◇— Teacher; College ◇— Student | House ◆— Room; Order ◆— OrderLine | Student ⇢ College (a method parameter) |
| In code | the whole keeps references passed in from outside | the whole creates and owns its parts | a parameter, local variable or return type |
Relationships
Aggregation, composition and dependency (cont.)
class House { // composition
private List<Room> rooms = new ArrayList<>();
House() { rooms.add(new Room("Kitchen")); } // parts created inside
}
class Department { // aggregation
private List<Teacher> teachers;
void add(Teacher t) { teachers.add(t); } // parts come from outside
}Java prints nothing for class declarations; what this code means is the diagram above.
Concept 8 of 15
Generalization and abstract classes
Concept 8 of 15
Generalization and abstract classes (cont.)
abstract class Person {
private String name, address;
abstract void printInfo();
}
class Employee extends Person { ... }- name, - address and the italic printInfo(), and Employee joined to it by a hollow-triangle arrow (extends).- Generalization is inheritance: the child is a kind of the parent and inherits its attributes and operations. Draw a solid line with a hollow triangle pointing to the parent.
- An abstract class cannot be instantiated; its name is written in italics (or marked
{abstract}). - An abstract operation has no body; it is also italic, and each concrete subclass must implement it.
- Draw one triangle and merge the lines when several classes share a parent.
Extended class diagrams
Interfaces and realization
Extended class diagrams
Interfaces and realization (cont.)
interface Payable {
boolean pay(double amount);
void refund(String id);
}
class DebitCard implements Payable { ... }«interface» Payable with + pay(amount : double) : boolean and + refund(id : String) : void, and DebitCard joined to it by a dashed hollow-triangle arrow (implements).- An interface is a named set of operations with no implementation — a contract. Show it as a class box with
«interface»above the name. - Realization (implementation): a dashed line with a hollow triangle from the class to the interface. The class promises to implement every operation.
- A class can realize many interfaces but (in Java) extend only one class.
- Example from the manual: an
Ownerinterface withacquire(property)anddispose(property), realized by both Person and Corporation — in very different ways.
Interactive
Name the relationship
Concept 11 of 15
Example: a telephone
Concept 11 of 15
Example: a telephone (cont.)
- A Telephone aggregates a Ringer, a CallerId and an AnsweringMachine (hollow diamonds at Telephone).
- The AnsweringMachine is composed of its recorded Messages (filled diamond): delete the machine and the messages go with it.
- A Line connects 0..* telephones; each telephone is on 0..1 line.
- Every attribute is private (
-) with public operations (+).
Worked diagrams
Example: the ATM system
Worked diagrams
Example: the ATM system (cont.)
Redrawn from the manual’s Figure X.6. The two inheritance trees (accounts and transactions) and the multiplicities are the parts examiners check first.
Concept 13 of 15
How to design a class diagram
- Identify the classes. Underline the nouns in the SRS and use case descriptions; keep the ones the system must remember (Account, Card), drop synonyms, attributes (balance) and actors that are outside the system.
- Add attributes — facts about each class (name, address, dob).
- Add operations — the verbs, and every message from your sequence diagrams.
- Find relationships: “has / owns” → association or aggregation/composition; “is a kind of” → generalization; “implements” → realization; “uses briefly” → dependency.
Concept 13 of 15
How to design a class diagram (cont.)
- Add multiplicities at both ends of every association.
- Review against the use cases: can every scenario be carried out with these classes?
Noun analysis on the ATM description
“A customer inserts a debit card into the ATM. The bank checks the PIN and the balance of the account. Each transaction — a withdrawal or transfer — is recorded with its date.”
Classes: Customer, DebitCard, ATM, Bank, Account, ATMTransaction, Withdrawal, Transfer. Attributes: PIN (of card), balance (of account), date (of transaction).
Concept 14 of 15
Common mistakes
| Mistake | Fix |
|---|---|
| Plural class names (Students) | singular: Student |
| Actors or the whole system drawn as classes (“ATMSystem”, “User interface”) | model the domain objects instead |
Attributes that are really other classes (account : String in Customer) | draw an association to Account |
| Associations without multiplicity | add it at both ends |
| Composition used for everything | composition only when parts die with the whole |
| Triangle pointing to the child | it points to the parent |
| Operations with no parameters or types | write withdraw(amount : double) : boolean |
| Class diagram and sequence diagram disagree | every message ↔ an operation of the receiver |
Your turn
Your turn: the lab tasks
Student Registration System (Lab IX)
The manual says it must include students, courses, registration, payment, requirements and transactions; a semester contains courses; courses can be registered, dropped, withdrawn, passed or failed; semesters can be frozen or attended; there are undergraduate (BCS, MCS) and postgraduate (MPhil, PhD) students who enrol in different ways.
- Which sentence suggests generalization?
- Is Semester–Course aggregation or composition? Defend it.
- Where do the verbs (register, drop, withdraw…) become operations?
- Which multiplicities apply between Student, Registration and Course?
Library Management System (Lab X)
- Candidate classes come from the nouns: user/member (student, staff), librarian, library card, book, reservation, loan, fine, feedback.
- Which are kinds of something else?
- Does a Fine exist without a Loan?
- Use your sequence diagrams: every message must appear as an operation.
Worked example 1 of 3
Worked example 1
public class Book { private String isbn; private String title; private static int count; public boolean isAvailable() { … } }Show the solution
- isbn : String, - title : String, - count : int (underlined, because static). Operation: + isAvailable() : boolean.Worked example 2 of 3
Worked example 2
Show the solution
Worked example 3 of 3
Worked example 3
calculateInterest() differently. Model it.Show the solution
- accountNo : String, - balance : double and the abstract operation + calculateInterest() : double. SavingsAccount and CurrentAccount each generalize to Account (hollow triangle at Account) and implement calculateInterest().Practice
Practice (1 of 2)
Try each one on paper first, then check your answer.
+, -, # and ~ mean in front of an attribute?Show the answer
public double getBalance().Show the answer
+ getBalance() : doubleShow the answer
Practice
Practice (2 of 2)
Try each one on paper first, then check your answer.
Show the answer
Show the answer
Customer 1 —— owns —— 1..* Account.Show the answer
Check yourself
Quiz
Resources
Further learning
Hand-picked material to go deeper. Links open in a new tab.
Resources
Further learning (cont.)
Summary
Summary
- A class box has name, attributes (visibility name : type) and operations (visibility name(params) : type).
- Visibility: + public, - private, # protected, ~ package; underline = static, italic = abstract.
- Association (with multiplicity) < aggregation (◇, independent parts) < composition (◆, parts die with the whole); dependency is a short “uses”.
- Generalization (hollow triangle to the parent) = inheritance; realization (dashed) = implementing an interface.
- Find classes from nouns, operations from verbs and sequence-diagram messages.
- Check the class diagram against the use cases and sequence diagrams.