Association, Composition and Aggregation
✅ 1. Association :
- Association is a relationship between two or more classes (objects) that shows how they are connected.
- Unlike composition or aggregation, association does not imply ownership or dependency — it just represents a link.
- Associations can be:
- One-to-One
- One-to-Many
- Many-to-One
- Many-to-Many
๐ Real-life Example :
- A Student is associated with a Faculty.
- The Faculty teaches Students, and Students learn from the Faculty.
- Both can exist independently, but they share a relationship.
Student ↔ Faculty
(Simple line = Association)class Student {
private String name;
Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Faculty {
private String name;
Faculty(String name) {
this.name = name;
}
public void teach(Student student) {
System.out.println(name + " teaches " + student.getName());
}
}
public class AssociationExample {
public static void main(String[] args) {
Student s1 = new Student("Ravi");
Faculty f1 = new Faculty("Dr. Sharma");
// Association: Faculty teaches Student
f1.teach(s1);
}
}Dr. Sharma teaches Ravi- Association just connects objects — neither object’s lifecycle depends on the other.
- If one is destroyed, the other can still exist.
Composition vs Aggregation
When designing classes in Object-Oriented Programming (OOP), we often deal with “has-a” relationships. These can be either Composition or Aggregation. Although both represent associations, the key difference lies in the dependency of objects.
✅ 2. Composition
- Definition: A strong “has-a” relationship.
- The contained object cannot exist without the container.
- If the container is destroyed, the contained objects are also destroyed.
๐Real-life Example:
- A University consists of several Departments.
- If the University object is destroyed, all its Department objects will also be destroyed.
- Since a Department cannot exist without its University, this is a strong association, i.e., composition.
University ◆── Department (black diamond= Composition)
class Department {
private String name;
Department(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class University {
private List<Department> departments;
University(List<Department> departments) {
this.departments = departments;
}
public void showDepartments() {
for (Department d : departments) {
System.out.println(d.getName());
}
}
}
public class CompositionExample {
public static void main(String[] args) {
Department d1 = new Department("Computer Science");
Department d2 = new Department("Mechanical");
List<Department> deptList = List.of(d1, d2);
University university = new University(deptList);
university.showDepartments();
// When university is destroyed, departments go away too.
}
}
✅ 3. Aggregation :
- Definition: A weak “has-a” relationship.
- The contained object can exist independently of the container.
- Destroying the container does not destroy the contained objects.
๐ Real-life Example
- A Department may have several Professors.
- If the Department is closed, the Professor objects can still exist independently (they may move to another department).
- Since a Professor can exist without a specific Department, this is a weak association, i.e., aggregation.
Department ◇── Professor
(white diamond = Aggregation)class Professor { private String name; Professor(String name) { this.name = name; } public String getName() { return name; } } class DepartmentWithProfessor { private List<Professor> professors;//Aggregation DepartmentWithProfessors(String deptName, List<Professor> professors) { this.deptName = deptName; this.professors = professors; } public void showProfessors() { System.out.println("Department: " + deptName); for (Professor p : professors) { System.out.println("Professor: " + p.getName()); } } } public class AggregationExample { public static void main(String[] args) { Professor p1 = new Professor("Dr. Sharma"); Professor p2 = new Professor("Dr. Singh"); List<Professor> profList = List.of(p1, p2); DepartmentWithProfessors dept = new DepartmentWithProfessors("Computer Science", profList); dept.showProfessors(); // Even if Department is removed, Professors still exist. } }
๐ Key Differences Between Composition and Aggregation:
Feature
Composition (Strong)
Aggregation (Weak)
Dependency
Contained objects depend on container
Contained objects independent
Lifetime
Destroyed when container is destroyed
Can exist even if container is destroyed
UML Notation
Black Diamond (◆)
White Diamond (◇)
Example
University – Departments
Department – Professors
๐ Final Thoughts
- Association is a general relationship between two objects. Both can exist independently, but they are linked (e.g., Student ↔ Faculty).
- Aggregation is a special type of association where objects have a weak dependency (e.g., Department ↔ Professor). The contained object can still exist independently.
- Composition is a strong form of association where objects have a strong dependency (e.g., University ◆ Department). If the container is destroyed, the contained objects are destroyed too.





