A class is drawn as a box with three parts: name, attributes, methods.
Visibility signs: + public, - private, # protected. Relationship lines join the boxes with the symbols in the comparison table in 4.4.
Writing attributes and methods in UML
UML writes the name first and the type after a colon, which is the opposite order to Java:
| UML | Java |
|---|---|
- name : String | private String name; |
- balance : double = 0 | private double balance = 0; |
+ getName() : String | public String getName() |
+ deposit(amount : double) : void | public void deposit(double amount) |
# speed : int | protected int speed; |
~ code : String | String code; (default, package access) |
Some other conventions:
- A static member is underlined.
- An abstract class or method is written in italics (or marked
{abstract}). - An interface has
«interface»above its name. - The constructor is often listed with the methods, without a return type:
+ Student(name : String).
Relationship lines
| Relationship | Line | Example |
|---|---|---|
| Inheritance (extends) | Solid line with a hollow triangle pointing to the parent | Dog to Animal |
| Realisation (implements) | Dashed line with a hollow triangle pointing to the interface | Duck to Flyable |
| Association | Solid line, with an optional arrow showing direction | Student to Teacher |
| Dependency (uses briefly) | Dashed line with an open arrow | Report to Printer |
| Aggregation | Solid line with a hollow diamond at the whole | Department to Teacher |
| Composition | Solid line with a filled diamond at the whole | Car to Engine |
Multiplicity
Numbers at each end of a line say how many objects take part:
| Notation | Meaning |
|---|---|
1 | Exactly one |
0..1 | None or one (optional) |
* or 0..* | Any number, including none |
1..* | At least one |
2..4 | From two to four |
For example, a line from Library to Book with 1 at the Library end and 0..* at the Book end means: each library has any number of books, and each book is in exactly one library.
From diagram to code
This class box translates directly into Java:
+--------------------------------+
| BankAccount |
+--------------------------------+
| - accountNo : String |
| - balance : double |
+--------------------------------+
| + BankAccount(no : String) |
| + deposit(amount : double) |
| + getBalance() : double |
+--------------------------------+public class BankAccount {
private String accountNo;
private double balance;
public BankAccount(String no) { this.accountNo = no; }
public void deposit(double amount) { balance += amount; }
public double getBalance() { return balance; }
}Drawing the class diagram before you code helps you agree on the classes, their responsibilities and their links. It is much cheaper to change a box on paper than a hundred lines of code.
Remember
- Three compartments: name, attributes, methods. Visibility:
+public,-private,#protected,~package. - UML writes
name : Type; Java writesType name. - Triangles mean inheritance or realisation; diamonds mean aggregation (hollow) or composition (filled).