UML Class Diagrams
BeginnerLearn UML class diagram notation — association, aggregation, composition, inheritance, and realization — with Java code equivalents for each relationship.
Overview
A UML Class Diagram visually captures the static structure of a system: classes, their attributes and methods, and the relationships between them. Five core relationships matter most in interviews: Association (a class uses or references another), Aggregation (has-a with independent lifecycle — hollow diamond), Composition (owns-a with dependent lifecycle — filled diamond), Inheritance (is-a — extends), and Realization (implements an interface). Multiplicity annotations (1..1, 0..*, 1..*) express cardinality. Mastering UML class diagrams is prerequisite to communicating LLD designs clearly on a whiteboard.
Requirements Analysis
UML relationships map directly to Java keywords: extends (inheritance), implements (realization), a field of another type (association/aggregation/composition). The distinction between aggregation and composition lies in lifecycle — composition means the child cannot exist independently of the parent.
// Association : A has a reference to B, but A does not own B's lifecycle
// Aggregation : A has-a B (hollow diamond) — B can exist without A
// Composition : A owns-a B (filled diamond) — B is destroyed when A is destroyed
// Inheritance : A extends B (is-a relationship)
// Realization : A implements B (can-do capability)Core Classes & Relationships
Multiplicity in UML: 1 (exactly one), 0..1 (zero or one), 0..* (zero or many), 1..* (one or many). A Library aggregates Books (books exist independently). A House composes Rooms (rooms cannot exist without a house). A Dog extends Animal (inheritance). A Flyable interface is realized by Bird.
// ── Inheritance (is-a) ───────────────────────────────────────────────
// UML: Animal <|-- Dog (open arrowhead, solid line)
public abstract class Animal {
protected String name;
public abstract void speak();
}
public class Dog extends Animal {
@Override public void speak() { System.out.println("Woof!"); }
}
// ── Realization / Interface (can-do) ─────────────────────────────────
// UML: Flyable <|.. Bird (open arrowhead, dashed line)
public interface Flyable { void fly(); }
public interface Swimmable { void swim(); }
public class Duck extends Animal implements Flyable, Swimmable {
@Override public void speak() { System.out.println("Quack"); }
@Override public void fly() { System.out.println("Duck flying"); }
@Override public void swim() { System.out.println("Duck swimming"); }
}
// ── Association (uses-a) ──────────────────────────────────────────────
// UML: Driver --> Car (open arrowhead, solid line, no diamond)
public class Driver {
private Car car; // Driver uses Car, but does not own its lifecycle
public void drive(Car car) { this.car = car; car.start(); }
}Java Implementation
Aggregation: Library holds a list of Books — books can exist outside the library. Composition: House holds a list of Rooms — rooms are created inside House and destroyed with it. The difference in Java is constructor-vs-field injection: composition creates child objects internally; aggregation receives them from outside.
// ── Aggregation (has-a, hollow diamond) ──────────────────────────────
// UML: Library o-- Book (hollow diamond on Library side)
// Book can exist independently of Library
public class Book {
private final String isbn;
private final String title;
public Book(String isbn, String title) { this.isbn = isbn; this.title = title; }
public String getTitle() { return title; }
}
public class Library {
private final List<Book> books = new ArrayList<>(); // aggregation — books passed in
public void addBook(Book book) { books.add(book); }
public void removeBook(Book book) { books.remove(book); }
// Books are NOT destroyed when Library is destroyed
}
// Usage
Book b1 = new Book("978-0", "Clean Code");
Book b2 = new Book("978-1", "Effective Java");
Library lib = new Library();
lib.addBook(b1);
lib.addBook(b2);
// b1 and b2 still exist after lib is garbage collected
// ── Composition (owns-a, filled diamond) ─────────────────────────────
// UML: House *-- Room (filled diamond on House side)
// Room CANNOT exist without a House
public class Room {
private final String name;
private final int areaSqFt;
Room(String name, int area) { this.name = name; this.areaSqFt = area; } // package-private
public String getName() { return name; }
}
public class House {
private final List<Room> rooms; // composition — rooms created inside House
public House(int bedrooms, int bathrooms) {
rooms = new ArrayList<>();
for (int i = 0; i < bedrooms; i++) rooms.add(new Room("Bedroom " + (i+1), 200));
for (int i = 0; i < bathrooms; i++) rooms.add(new Room("Bathroom " + (i+1), 50));
}
public List<Room> getRooms() { return Collections.unmodifiableList(rooms); }
// When House is garbage collected, all Rooms are too
}
// ── Multiplicity Summary ──────────────────────────────────────────────
// Person (1) --- (0..*) Address : one person, zero or many addresses
// Order (1) --- (1..*) OrderItem : one order, one or more items
// Employee (0..1) --- (1) Department : employee may or may not belong to one departmentKey Points to Remember
- 1Composition: child is created INSIDE parent constructor and cannot exist independently — "owns-a" (filled diamond).
- 2Aggregation: child is passed from OUTSIDE parent and can exist independently — "has-a" (hollow diamond).
- 3Realization: a class implements an interface and provides all its methods — dashed line with open arrowhead in UML.
- 4Multiplicity annotations on associations answer: "how many instances of B are related to one instance of A?"
Interview Questions
Sign in to ask AriaWhat is the difference between aggregation and composition in UML? Give a Java code example.
Draw a UML class diagram for a University with Students, Courses, and Professors.
When would you use realization (interface) vs inheritance (abstract class) in a UML design?
Ask Aria about UML Class Diagrams
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.