Skills101

OOP Concepts · Lesson 5 of 8 · Updated ·

Association, Aggregation and Composition — is-a vs has-a

Classes rarely live alone. A Teacher teaches Students, a Team has Players, a House has Rooms. OOP names these relationships, and interviewers like to ask the difference.

Table: inheritance is-a (Car is a Vehicle); association uses (Teacher and Student, independent); aggregation weak has-a (Team has Players, parts survive); composition strong has-a (House has Rooms, parts die too)

1. is-a vs has-a

  • is-a → inheritance: a Manager is an Employee.
  • has-a → one object holds or uses another: a Car has an Engine.

has-a comes in three strengths: association → aggregation → composition.

2. Association: "uses" (independent)

Two objects work together, but neither owns the other. Both can exist alone.

class Student:
    def __init__(self, name): self.name = name

class Teacher:
    def __init__(self, name): self.name = name
    def evaluate(self, student, marks):
        return f"{self.name} gave {student.name} {marks} marks"

print(Teacher("Mr. Rao").evaluate(Student("Asha"), 92))
Mr. Rao gave Asha 92 marks

3. Aggregation: "has-a" (weak, parts can live alone)

The container holds references to parts that were created elsewhere and outlive the container.

class Player:
    def __init__(self, name): self.name = name

class Team:
    def __init__(self, name, players):
        self.name = name
        self.players = players          # players passed in from outside

kohli, bumrah = Player("Kohli"), Player("Bumrah")
team = Team("India", [kohli, bumrah])
del team                                 # the team is gone…
print(kohli.name, "still exists")        # …but players are not
Kohli still exists

4. Composition: "owns" (strong, parts die with the whole)

The container creates and owns its parts. Without the container, the parts make no sense.

import java.util.*;

class Room {
    String name;
    Room(String name) { this.name = name; }
}

class House {
    private final List<Room> rooms = new ArrayList<>();
    House(String... roomNames) {
        for (String n : roomNames) rooms.add(new Room(n));   // House creates its rooms
    }
    int roomCount() { return rooms.size(); }
}

public class Main {
    public static void main(String[] args) {
        House h = new House("Hall", "Kitchen", "Bedroom");
        System.out.println(h.roomCount() + " rooms; nobody else holds them");
    }
}
3 rooms; nobody else holds them

When the house object is gone, its rooms go too — they were never shared.

5. The quick test

Ask: "If I delete the whole, do the parts still make sense on their own?"

ExampleParts survive?Relationship
Department — Professorsyes (they can join another dept)aggregation
Order — OrderItemsno (an item belongs to one order)composition
Doctor — Patientboth independent, just interactassociation
Human — Heartnocomposition
Playlist — Songsyesaggregation

In UML diagrams: association = plain line, aggregation = hollow diamond, composition = filled diamond (on the "whole" side).

Common mistakes

  • Using inheritance for has-a relationships (Car extends Engine ✗).
  • Thinking aggregation and composition are the same — the difference is ownership and lifetime.

Quick recap

  • is-a → inheritance; has-a → association / aggregation / composition.
  • Association: independent objects that interact.
  • Aggregation: whole has parts that can live alone (hollow diamond).
  • Composition: whole owns parts that die with it (filled diamond).

Practice what you learned

📝 Quiz: Object Relationships5 questions · instant answers & explanations · +10 XP · counts towards your free certificateTake the quiz →