Aggregation

Has-a relationship (weak)

Overview

Aggregation is a specialized form of association representing a "has-a" relationship with weak ownership. The container object (whole) contains references to other objects (parts), but the parts can exist independently.

This is a "whole-part" relationship where the parts can survive without the whole.

Key Concepts

Weak "has-a" relationship

Parts can exist independently of the whole

Parts can be shared between multiple wholes

Represented by hollow diamond in UML

Lifecycle independent - deleting whole doesn't delete parts

Code Example

java
1// Aggregation: Department has Employees
2// Employees can exist without the Department
3
4public class Employee {
5    private String name;
6    private String employeeId;
7    
8    public Employee(String name, String employeeId) {
9        this.name = name;
10        this.employeeId = employeeId;
11    }
12    
13    public String getName() { return name; }
14}
15
16public class Department {
17    private String name;
18    private List<Employee> employees; // Aggregation - weak ownership
19    
20    public Department(String name) {
21        this.name = name;
22        this.employees = new ArrayList<>();
23    }
24    
25    // Employees are passed in - not created by Department
26    public void addEmployee(Employee employee) {
27        employees.add(employee);
28        System.out.println(employee.getName() + " added to " + name);
29    }
30    
31    public void removeEmployee(Employee employee) {
32        employees.remove(employee);
33        // Employee still exists after removal!
34        System.out.println(employee.getName() + " removed from " + name);
35    }
36    
37    public void listEmployees() {
38        System.out.println("Department: " + name);
39        for (Employee emp : employees) {
40            System.out.println("  - " + emp.getName());
41        }
42    }
43}
44
45// Another example: Playlist and Songs
46public class Song {
47    private String title;
48    private String artist;
49    
50    public Song(String title, String artist) {
51        this.title = title;
52        this.artist = artist;
53    }
54}
55
56public class Playlist {
57    private String name;
58    private List<Song> songs; // Aggregation
59    
60    public Playlist(String name) {
61        this.name = name;
62        this.songs = new ArrayList<>();
63    }
64    
65    public void addSong(Song song) {
66        songs.add(song);
67        // Song can be in multiple playlists
68    }
69}
70
71// Usage
72public class Main {
73    public static void main(String[] args) {
74        // Create employees independently
75        Employee alice = new Employee("Alice", "E001");
76        Employee bob = new Employee("Bob", "E002");
77        
78        // Create departments
79        Department engineering = new Department("Engineering");
80        Department marketing = new Department("Marketing");
81        
82        // Aggregate employees
83        engineering.addEmployee(alice);
84        engineering.addEmployee(bob);
85        
86        // Bob can move to marketing - doesn't destroy bob
87        engineering.removeEmployee(bob);
88        marketing.addEmployee(bob);
89        
90        // If we delete engineering department, alice still exists
91    }
92}

Employees exist independently and can be moved between departments. Deleting a Department doesn't delete its Employees.

Real-World Example

  • University and Professors: University has professors, but professors exist independently and can teach at multiple universities
  • Library and Books: Library has books, but books can be moved to another library
  • Team and Players: Team has players, but players can be traded to other teams

Best Practices

  • 1.

    Use aggregation when parts can exist without the whole

  • 2.

    Parts are typically passed to the whole (not created inside)

  • 3.

    Use when parts can be shared between multiple wholes

  • 4.

    Don't use when deleting whole should delete parts

💡 Interview Tips

  • Explain it's a weak "has-a" relationship

  • Parts have independent lifecycle

  • Give Department-Employee example

  • Know UML notation (hollow diamond)

  • Compare with composition (strong ownership)

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In
Aggregation - Class Relationships | LLD | Revise Algo