Class Relationships

Aggregation

A weak form of association where the part can exist independently of the whole. When the whole is destroyed, the parts continue to exist.

8 min readEssential

1What is Aggregation?

Real-World Analogy
Team and Players

A team HAS players. But if the team is dissolved, players still exist - they can join other teams or play independently. Players have their own lifecycle separate from the team.

Department and Employees

A department HAS employees. If the department is shut down, employees are not destroyed - they get transferred to other departments or leave the company. They exist independently.

Aggregation is a weak HAS-A relationship where the part can exist independently of the whole. The whole does not own the parts - it only references them. When the whole is destroyed, the parts continue to exist elsewhere.

UML Notation: Aggregation

Team
Player
Empty (hollow) diamond = Aggregation
Diamond is on the "whole" side (Team)

2Aggregation vs Composition

Both represent HAS-A relationships, but differ in ownership and lifecycle dependency.

Aggregation (Weak)
  • Part CAN exist independently
  • Part is passed INTO the whole
  • Whole does NOT create the part
  • Part survives destruction of whole
Example: Team - Player
Composition (Strong)
  • Part CANNOT exist independently
  • Part is CREATED BY the whole
  • Whole owns and controls the part
  • Part destroyed with the whole
Example: Car - Engine
AspectAggregationComposition
RelationshipHas-A (weak)Has-A (strong)
OwnershipPartial / NoneFull
Part lifecycleIndependentDepends on whole
Part creationExternal / Passed inBy the whole
UML SymbolEmpty diamondFilled diamond

3Implementation

Aggregation Example - Team and Players
// ========== Aggregation: Team has Players ==========

// Part class - exists independently of Team
class Player {
    private String name;
    private int jerseyNumber;

    public Player(String name, int jerseyNumber) {
        this.name = name;
        this.jerseyNumber = jerseyNumber;
    }

    public String getName() { return name; }
    public int getJerseyNumber() { return jerseyNumber; }

    public void play() {
        System.out.println(name + " (#" + jerseyNumber + ") is playing");
    }
}

// Whole class - has Players but doesn't own them
class Team {
    private String name;
    private List<Player> players;  // Aggregation - Team has Players

    public Team(String name) {
        this.name = name;
        this.players = new ArrayList<>();
    }

    // Players are PASSED IN, not created by Team
    public void addPlayer(Player player) {
        players.add(player);
        System.out.println(player.getName() + " joined " + name);
    }

    public void removePlayer(Player player) {
        players.remove(player);
        System.out.println(player.getName() + " left " + name);
    }

    public void playMatch() {
        System.out.println(name + " is playing a match!");
        for (Player player : players) {
            player.play();
        }
    }
}

// Usage
public class Main {
    public static void main(String[] args) {
        // Players exist independently
        Player messi = new Player("Messi", 10);
        Player ronaldo = new Player("Ronaldo", 7);

        // Team aggregates (references) players
        Team barcelona = new Team("Barcelona");
        barcelona.addPlayer(messi);
        barcelona.playMatch();

        // Team is destroyed, but players still exist
        barcelona = null;
        
        // Messi can join another team
        Team psg = new Team("PSG");
        psg.addPlayer(messi);  // Messi still exists!
        psg.addPlayer(ronaldo);
        psg.playMatch();
    }
}
Output:
Messi joined Barcelona
Barcelona is playing a match!
Messi (#10) is playing
Barcelona team disbanded
Messi joined PSG
Ronaldo joined PSG
PSG is playing a match!
Messi (#10) is playing
Ronaldo (#7) is playing
Key Implementation Difference
In aggregation, the part object is passed into the whole (usually via constructor or setter). In composition, the whole creates the part internally. This is how you distinguish them in code.

4Common Examples

LibraryBook
Library has books, but books exist independently. Same book can be in different libraries over time.
SchoolStudent
School has students, but students exist independently. They can transfer to other schools.
CompanyEmployee
Company has employees, but employees exist independently. They can leave and join other companies.
PlaylistSong
Playlist has songs, but songs exist independently. Same song can be in multiple playlists.

5Interview Follow-up Questions

Interview Follow-up Questions

Common follow-up questions interviewers ask

6Key Takeaways

1Aggregation = weak HAS-A where parts can exist independently.
2Parts are passed into the whole, not created by it.
3Part lifecycle is independent of whole.
4UML uses empty (hollow) diamond on the whole side.
5Examples: Team-Player, Library-Book, Department-Employee.
6Contrast with composition where parts cannot exist without whole.