- 1UML Relationships in Java: Association, Aggregation, Composition Explained with Examples
- 2UML Inheritance and Generalization | Java Examples for Beginners
- 3Interfaces vs Abstract Classes in UML with Java Examples
- 4Multiplicity and Navigability in UML – Easy Guide with Real-Life Examples
- 5Constraints and Notes in UML – Beginner-Friendly Guide with Java Examples
Relationships in UML: Association, Aggregation, and Composition
Introduction
In the world of software design, everything is connected. Objects don’t exist in isolation — they interact, depend on, and manage each other. UML helps us visualize these connections through different types of relationships. Three of the most fundamental relationships in object-oriented design are Association, Aggregation, and Composition.
Understanding these relationships will help you model real-world scenarios more effectively and map them into Java code with clarity and precision.
Why UML Relationships Matter
- They define how objects collaborate
- They reflect object lifecycle dependencies
- They clarify ownership and containment
- They aid in designing loosely or tightly coupled systems
Relationship Overview

1. Association
Association is the most basic relationship. It defines a general connection between two independent classes. Both objects can exist independently of each other.
Example: Student and Teacher
A student may be taught by multiple teachers, and each teacher can teach many students.
class Student {
String name;
List<Teacher> teachers;
}
class Teacher {
String name;
List<Student> students;
}
In UML, association is represented by a simple line connecting the two classes. Multiplicity (like 1..*, 0..1) is used to denote how many instances can be associated.

Key Traits of Association
- Loosely coupled
- No ownership implied
- Objects can exist independently
2. Aggregation
Aggregation is a specialized form of association. It represents a "whole-part" relationship where the part can exist independently of the whole. It shows a has-a relationship.
Example: Department and Teachers
A department has teachers, but a teacher can exist independently (may teach in another department).
class Department {
String name;
List<Teacher> teachers;
}
class Teacher {
String name;
}
In UML, aggregation is shown with a hollow diamond at the end of the whole (Department).

Key Traits of Aggregation
- Represents whole-part relationship
- Parts can exist independently
- Shows shared ownership
3. Composition
Composition is a stronger form of aggregation. It implies ownership and a strict lifecycle dependency. If the whole is destroyed, the parts are also destroyed.
Example: School and ClassRooms
If a school is closed (deleted), its classrooms cease to exist — they have no meaning outside the school.
class School {
String name;
List<ClassRoom> classRooms;
School() {
classRooms = new ArrayList<>();
}
}
class ClassRoom {
String roomNumber;
ClassRoom(String roomNumber) {
this.roomNumber = roomNumber;
}
}
In UML, composition is shown with a filled black diamond at the end of the whole (School).

Key Traits of Composition
- Represents strong ownership
- Parts cannot exist without the whole
- Destruction of whole leads to destruction of parts
Comparison Table
Relationship | Direction | Lifetime | UML Notation | Java Example |
---|---|---|---|---|
Association | Bidirectional or Unidirectional | Independent | Simple Line | Student - Teacher |
Aggregation | Whole to Part | Independent | Hollow Diamond | Department - Teacher |
Composition | Whole to Part | Dependent | Filled Diamond | School - ClassRoom |
When to Use Each Relationship
Use Association when:
- Objects are related but don’t depend on each other
- You want flexible, loosely coupled classes
Use Aggregation when:
- One object is a logical container for another
- The contained object can exist outside the container
Use Composition when:
- The part object cannot exist without the whole
- You want to tightly couple the lifecycle
Practice Scenario: School System Design
Let’s map out relationships in a school:
- Association: Student ↔ Teacher (they interact but live separately)
- Aggregation: Department → Teacher (teacher can belong to multiple departments)
- Composition: School → ClassRoom (classroom depends on school)

Conclusion
Modeling relationships correctly is the backbone of good object-oriented design. UML’s ability to express Association, Aggregation, and Composition gives us the tools to mirror the real world — complete with ownership, lifecycle, and hierarchy.
By choosing the right relationship for your classes in both diagrams and Java code, you create designs that are not only accurate but also maintainable, scalable, and intuitive. As you continue modeling, practice recognizing these relationships in everyday systems. That’s the key to mastering UML at a professional level.
Comments
Loading comments...