Unit 4 · Association, Aggregation and Composition

4.4.2Class diagrams

A class is drawn as a box with three parts: name, attributes, methods.

Student- name: String- marks: int+ getGrade(): String+ setMarks(int)class nameattributes (state)methods (behaviour)+public-private#protected~package (default)visibilityformat: name: Type and method(params): ReturnType
A UML class box: name, attributes and methods, with visibility signs + public, - private, # protected

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:

UMLJava
- name : Stringprivate String name;
- balance : double = 0private double balance = 0;
+ getName() : Stringpublic String getName()
+ deposit(amount : double) : voidpublic void deposit(double amount)
# speed : intprotected int speed;
~ code : StringString code; (default, package access)

Some other conventions:

Relationship lines

RelationshipLineExample
Inheritance (extends)Solid line with a hollow triangle pointing to the parentDog to Animal
Realisation (implements)Dashed line with a hollow triangle pointing to the interfaceDuck to Flyable
AssociationSolid line, with an optional arrow showing directionStudent to Teacher
Dependency (uses briefly)Dashed line with an open arrowReport to Printer
AggregationSolid line with a hollow diamond at the wholeDepartment to Teacher
CompositionSolid line with a filled diamond at the wholeCar to Engine

Multiplicity

Numbers at each end of a line say how many objects take part:

NotationMeaning
1Exactly one
0..1None or one (optional)
* or 0..*Any number, including none
1..*At least one
2..4From 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:

Text
+--------------------------------+
|           BankAccount          |
+--------------------------------+
| - accountNo : String           |
| - balance : double             |
+--------------------------------+
| + BankAccount(no : String)     |
| + deposit(amount : double)     |
| + getBalance() : double        |
+--------------------------------+
Java
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 writes Type name.
  • Triangles mean inheritance or realisation; diamonds mean aggregation (hollow) or composition (filled).