A UML class diagram is a standardized visual representation of a class's structure. In IB Computer Science B3.1, it shows the class name, its attributes and its methods, usually in a rectangle divided into three compartments.
How a UML class diagram works
Unified Modeling Language (UML) provides a common notation for designing and documenting object-oriented programs. A class diagram describes a class, which is a blueprint from which objects are instantiated; it does not display the changing values held by one particular object.
| Compartment | What it contains | Example |
|---|---|---|
| Class name | The identifier of the class | Student |
| Attributes | Data stored by each object | -name: String |
| Methods | Behaviours available to objects | +getName(): String |
An attribute is commonly written as visibility name: dataType. A method is commonly written as visibility methodName(parameters): returnType.
Visibility symbols indicate where an attribute or method can be accessed:
| Symbol | Visibility | Meaning |
|---|---|---|
+ | Public | Accessible from outside the class |
- | Private | Accessible only within the class |
# | Protected | Accessible within the class and its subclasses |
For example, a BankAccount class could contain the private attribute -balance: Real and the public methods +deposit(amount: Real): void and +getBalance(): Real. Keeping balance private and controlling access through methods demonstrates encapsulation.
A constructor may also appear in the methods compartment, such as +BankAccount(initialBalance: Real). It initializes an object when that object is instantiated.
A common misconception is that a UML class diagram contains the program's complete algorithm. It represents the class's static structure and method signatures, not the detailed instructions inside each method.
Exam technique
When asked to draw or interpret a UML class diagram, use the correct three-compartment structure, include data types and method return types, and distinguish private attributes from public methods. Examiners may also ask you to explain how the diagram demonstrates encapsulation or how it could be implemented as a class in code.