What is the difference between association, aggregation and composition?

When designing software, you often need to define relationships between objects or classes. These relationships—association, aggregation, and composition—help model real-world connections in your code. Let's explore these concepts with a simple example before diving into the details.

Real-World Example

Imagine a school:

  1. Association: Teachers and students are part of the school. A teacher can teach multiple students, and a student can learn from multiple teachers. They are connected but independent—neither exists because of the other.
  2. Aggregation: The school has classrooms. Classrooms belong to the school, but they can exist independently. Even if the school closes, the classrooms may be used elsewhere.
  3. Composition: The school has desks in the classrooms. These desks cannot exist without the classrooms. If a classroom is removed, its desks are also destroyed.

Key Differences Between Association, Aggregation, and Composition

Association

  • Definition: A general relationship where two objects are connected but independent.
  • Characteristics:
    • Represents a "uses" or "works with" relationship.
    • Neither object owns the other.
  • Example: A teacher teaches students, but both can exist independently.
  • UML Representation: A plain line between objects.

Aggregation

  • Definition: A "has-a" relationship where one object contains another, but the contained object can exist independently.
  • Characteristics:
    • Represents a "whole-part" relationship.
    • The part can exist even if the whole is removed.
  • Example: A school has classrooms, but classrooms can exist without the school.
  • UML Representation: A hollow diamond at the "whole" end.

Composition

  • Definition: A stronger "has-a" relationship where one object contains another, and the contained object cannot exist independently.
  • Characteristics:
    • Represents a "whole-part" relationship with ownership.
    • The part is destroyed when the whole is destroyed.
  • Example: A classroom has desks that are destroyed if the classroom is removed.
  • UML Representation: A filled diamond at the "whole" end.

Practical Use in Code

Association

class Teacher: pass class Student: pass # Association: A teacher can teach students, but both exist independently. teacher = Teacher() student = Student()

Aggregation

class School: def __init__(self): self.classrooms = [] # Aggregates classrooms class Classroom: pass # Aggregation: Classrooms exist independently of the school. school = School() classroom = Classroom() school.classrooms.append(classroom)

Composition

class Classroom: def __init__(self): self.desks = [] # Composes desks class Desk: pass # Composition: Desks cannot exist without the classroom. classroom = Classroom() desk = Desk() classroom.desks.append(desk)

Summary

  • Association: Loose connection, objects are independent.
  • Aggregation: One object owns another but doesn't control its lifecycle.
  • Composition: Strong ownership, the lifecycle of the part depends on the whole.

To strengthen your system design and object-oriented programming skills, explore DesignGurus.io's Grokking System Design Fundamentals or Grokking the System Design Interview. These courses explain such concepts in practical scenarios.

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is PCB in OS?
What are the three pillars of frontend?
Why is multithreading faster?
What is QA industry standard?
What is the difference between multithreading and multiple cores?
Which mock interview is best?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.