UML Class Diagrams
UML Class Diagrams
Once a program has more than a handful of classes, reading the source code is a slow way to see how it is structured. A UML class diagram puts that structure on one page. It shows which classes exist, what each one stores and does, and how the classes connect. You use these diagrams when you plan a project and when you read design documents. Exam questions often ask you to translate between code and diagram in either direction.
Anatomy of a class box
A class diagram is a static view of a system that shows its classes, their attributes and operations, and the relationships between them. Each class is drawn as a rectangle with three compartments. The top one holds the class name, the middle one holds the attributes (fields), and the bottom one holds the operations (methods, including constructors).
An attribute is written visibility name: Type and an operation is written visibility name(parameter: Type): ReturnType. The type comes after a colon, which is the reverse of Java's double balance order. Each member begins with a visibility symbol:
Symbol | Visibility | Java keyword | Accessible from |
|---|---|---|---|
+ | public | public | any class |
- | private | private | the class itself only |
# | protected | protected | the class and its subclasses (and, in Java, its package) |
~ | package | no modifier | classes in the same package |
Two more conventions are common. A static member is underlined. An abstract class or operation is written in italics, or tagged {abstract} when italics are hard to draw by hand. An interface carries the label «interface» above its name.
Relationships between classes
The lines between boxes show how the classes relate. To read a line, look at its style and its arrowhead together:
Relationship | Drawn as | Meaning | Typical Java clue |
|---|---|---|---|
Association | solid line, optional open arrowhead | a lasting link between objects | a field that refers to another class |
Generalization | solid line, hollow triangle at the superclass | is-a (inheritance) | extends |
Realization | dashed line, hollow triangle at the interface | implements a contract | implements |
Dependency | dashed line, open arrowhead at the class used | uses briefly | a parameter or local variable |
The triangle always points at the more general type, never at the subclass. An arrowhead on an association shows navigability. For example, Order → Customer means an order knows its customer, though the customer may not know its orders. An association that ends in a diamond marks a whole made of parts. That is a specialized form of association with its own rules.
Multiplicity
Multiplicity is the range written at one end of an association. It states how many objects of that class can be linked to a single object at the other end. Common forms are 1 (exactly one), 0..1 (optional), 1..* (one or more), 0..* or * (any number), and bounded ranges such as 2..4.
Read each range from the far side. In Customer 1 ——— 0..3 Account, the 0..3 beside Account means one customer holds zero to three accounts. The 1 beside Customer means each account belongs to exactly one customer. Write each range beside the class it counts.
Every link connects one object on each side, so multiplicities let you bound totals. If each of n objects of class A links to between l and u objects of class B, the number of links L satisfies
Example: Suppose a bank has 200 customers and uses the 0..3 range above. It then has between 0 and 600 customer–account links. Each account has exactly one owner, so each link is a different account, and the bank holds at most 600 accounts.
What to remember
- A class box has three compartments (name, attributes, operations), and members use name: Type order.
- +, -, # and ~ mean public, private, protected and package.
- A solid line with a hollow triangle is inheritance, and a dashed line with a hollow triangle is interface realization. A dashed line with an open arrow is a dependency, and a plain solid line is an association.
- Multiplicity sits beside the class it counts, and nl≤L≤nu bounds the number of links.
Example: Turning library code into a class diagram
A library's loan system contains the Java classes below. A member may hold at most four active loans at once. Every loan belongs to exactly one member and refers to exactly one book. A book can be on at most one active loan.
public class Member {
private String name;
private Loan[] loans = new Loan[4];
public Loan borrow(Book b) { /* ... */ }
}
public class Loan {
private LocalDate due;
private Book book;
public boolean isOverdue() { /* ... */ }
}
public class Book {
private String isbn;
}
public class EBook extends Book {
private double fileSizeMB;
}a) Write the class box for Loan, showing all three compartments.
The name compartment holds Loan.
Both fields use the ________ modifier, so each attribute line starts with -. In UML the type follows the name after a colon.
The operation isOverdue is ________, so it starts with +. Its return type goes after the closing parenthesis and a colon.
- Name: Loan
- Attributes: - due: LocalDate and - book: Book
- Operations: + isOverdue(): boolean
Part b draws the book reference as an association line to Book. UML allows an association end to appear both as an attribute in the box and as a line, so keeping - book: Book is not wrong. Showing the same link twice adds clutter, though, so a clearer diagram usually keeps the association line and leaves the redundant attribute out of the box.
b) Name the relationship and give the multiplicity at each end for the pairs Member–Loan and Loan–Book. Then name the relationship between EBook and Book.
Member–Loan: a Member stores an array of Loan references, which is a lasting structural link. The relationship is therefore an association, drawn as a solid line. The range beside Loan counts loans per member; a member holds between zero and four loans, so it is 0..4. The range beside Member counts members per loan, and every loan belongs to exactly one member, so it is 1.
Loan–Book: the book field makes this an association as well. Each loan refers to exactly one book, so 1 goes beside Book. A book is on at most one active loan, so the range beside Loan is 0..1.
EBook–Book: EBook extends Book, so the relationship is a generalization. Draw a solid line from EBook that ends in a ________ touching Book.
The borrow(Book b) parameter also gives Member a dependency on Book. You can show it as a ________ line with an open arrowhead pointing at Book.
c) The library has 350 members and 1200 Book objects, with e-books included in that count. Use the multiplicities to decide whether every member can hold the maximum number of loans at the same time.
Start with the members. Each of n=350 members links to at most u=4 loans. The bound L≤nu therefore gives at most ________ active loans from the member side.
Now look at the books. The 0..1 range beside Loan lets each book support at most one active loan. That means 1200 books can support at most ________ active loans at once.
Every active loan must satisfy both limits, so the smaller one decides. The member side would allow ________ more loans than the books can supply.
No, every member cannot hold four loans at once. The library can support at most 1200 simultaneous loans.
A Java program declares public class SavingsAccount extends Account, and no interfaces are involved. How should this relationship appear on a UML class diagram that shows both classes?
In a UML class box, the attribute compartment can show a complete declaration for each attribute, not just its name. Arrange the parts of a full UML attribute declaration in the order they appear when read from left to right.
1.
The multiplicity in square brackets, such as [0..3]
2.
A colon followed by the attribute's type
3.
The visibility symbol, such as - for private
4.
An equals sign followed by the default value
5.
The attribute name