bigonotes
CS ConceptsOOP

Relationships: Association, Aggregation, Composition

How classes relate (is-a vs has-a), and how to tell association, aggregation and composition apart in UML and code.

4 min readLast updated

Classes rarely stand alone. In a design you need to say how they relate: is one a kind of another, or does one have or use another? Getting this right drives your class diagram, who creates which object, and what gets cleaned up when something is deleted.

Is-a vs has-a#

Start with the broadest split. Every relationship between two classes is one of two kinds:

  • Generalization (inheritance) is an is-a relationship. Penguin is a Bird. The child inherits from the parent.
  • Association is a has-a / uses-a relationship. One class holds a reference to, or works with, another.

Association is the umbrella term. It has two stronger, more specific forms based on ownership and lifecycle (whether the part is destroyed along with the whole). The table compares all three, including the UML (Unified Modeling Language) symbol you draw for each in a class diagram:

Relationship Meaning Lifecycle UML symbol
Association "uses / knows about" Fully independent Plain line (optionally an arrow)
Aggregation Weak has-a: a whole made of parts that can live on their own Part survives if the whole is destroyed Hollow diamond ◇ at the whole
Composition Strong has-a: the part belongs to exactly one whole Part is destroyed with the whole Filled diamond ◆ at the whole

The diagram shows the same three side by side.

Association, aggregation and composition side by side with their UML line styles and lifecycle rule

Aggregation (weak has-a)#

Aggregation is the weaker of the two ownership forms. The objects are linked but have separate lives: destroy the owner and the part survives.

Some everyday examples:

  • Car and Driver: a car has a driver, but if the car is scrapped the driver walks away and gets another car.
  • Classroom and Teacher: demolish the classroom and the teacher moves to another room.
  • School and Student: if the school closes, the students transfer elsewhere.

You can usually spot aggregation by where the part comes from. In code, the part is created outside and passed in (constructor or setter). Because someone else created it, it outlives the owner.

class Driver:
    def __init__(self, name: str = "John"):
        self.name = name


class Car:
    def __init__(self, driver: Driver):
        # Aggregation: Car refers to a Driver it didn't create.
        # The driver is passed in from outside (dependency injection).
        self._driver = driver


john = Driver()
car = Car(john)
del car           # the Car is gone...
print(john.name)  # ...but john is still referenced and alive: John

Composition (strong has-a)#

Composition is the strong form. The part cannot meaningfully exist without the whole: destroy the owner and the part goes with it.

Some everyday examples:

  • Body and Heart: a heart is part of exactly one body.
  • Car and its state: a car's state object (engine running, parked) only makes sense while that car exists.
  • House and Rooms: demolish the house and its rooms are gone.

The code clue is the opposite of aggregation. The owner usually creates the part itself and never hands it out to be shared, so it controls the lifecycle.

class Heart:
    def __init__(self):
        self.beats_per_minute = 70


class Body:
    def __init__(self):
        self._heart = Heart()  # created inside; no one else holds it


body = Body()
del body  # nothing else refers to the Heart, so it is freed too

Plain association#

Not every has-a or uses-a link involves a whole and its parts. Sometimes two classes work together with no ownership at all, e.g. a Teacher teaches many Students and a student has many teachers. Neither owns the other; that's plain association.

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


class Teacher:
    def grade(self, student: Student) -> None:
        # Uses a Student for the duration of the call, owns nothing
        print(f"Grading {student.name}")

Telling them apart#

When you're unsure which one you're looking at, ask these questions about the pair of classes:

Question Association Aggregation Composition
Is there a whole/part? No Yes Yes
Can the part be shared? n/a Yes No, one owner
Destroy owner, part...? Unaffected Survives Destroyed
Code hint Method parameter or loose reference Object passed into constructor/setter Part() created inside the owner

In interviews, prefer composition over inheritance for code reuse: "has-a" relationships are easier to change at runtime and avoid fragile class hierarchies.

Key takeaways#

  • Inheritance is is-a; association, aggregation and composition are has-a/uses-a.
  • Aggregation: the part lives independently (hollow diamond).
  • Composition: the part lives and dies with the whole (filled diamond).
  • Passed in from outside usually means aggregation; created inside usually means composition.
  • Favor composition over inheritance when you only need to reuse behavior.
Revision notes · 1 min

Core idea#

Two classes relate as is-a (generalization, inheritance) or has-a / uses-a (association). Association has two stronger forms, aggregation and composition, defined by ownership and lifecycle.

Key points#

  • Association is the umbrella term. In plain association, neither class owns the other, like Teacher and Student.
  • Aggregation is a weak has-a. The part survives if the whole is destroyed, like a car's driver.
  • Composition is a strong has-a. The part belongs to one whole and dies with it, like a body's heart.
  • The relationship drives the class diagram, who creates which object, and what gets cleaned up.
  • Passed in from outside usually means aggregation. Created inside the owner usually means composition.
  • Prefer composition over inheritance for code reuse. Has-a is easier to change at runtime and avoids fragile hierarchies.

Telling them apart#

Question Association Aggregation Composition
Whole/part? No Yes Yes
Part shared? n/a Yes No, one owner
Destroy owner, part...? Unaffected Survives Destroyed
Code hint Method parameter or loose reference Passed into constructor or setter Part() created inside the owner
UML symbol Plain line Hollow diamond at the whole Filled diamond at the whole

Pitfalls#

  • Using inheritance only to reuse behavior. Use composition.
  • Handing out a composed part to be shared. The owner then no longer controls its lifecycle.

Say it in an interview#

  • "Inheritance is is-a. Aggregation and composition are has-a."
  • "Aggregation: the part lives on. Composition: the part lives and dies with the whole."

Found a mistake? Email ask@bigonotes.com