Composition vs Aggregation vs Inheritance
Inheritance, composition and aggregation are three ways to relate classes. Inheritance models an is-a relationship, where a subclass is a specialized version of its superclass; composition models strong has-a ownership, where parts cannot exist without the whole; and aggregation models loose has-a relationships, where the referenced objects exist independently of the owner.
Inheritance isn't the only way to relate classes to each other, and it's often not the best one. Composition and aggregation are the two other standard relationships, and the real skill in object-oriented design is knowing which of the three actually fits a given situation.
The three relationships
Inheritance ("is-a"): a subclass is a specialized version of its superclass. A Car is a Vehicle.
Composition ("has-a", strong ownership): an object owns another object, and the owned object cannot meaningfully exist without its owner. A House has Rooms, and if the House is destroyed, its Rooms cease to exist too.
Aggregation ("has-a", loose ownership): an object holds a reference to another object, but that object has an independent existence outside the relationship. A University has Students, but a Student continues to exist perfectly well even if the University closes down.
A worked example
#include <iostream>
#include <vector>
using namespace std;
// Composition: Engine cannot exist meaningfully without a Car
class Engine {
public:
void start() { cout << "Engine starting..." << endl; }
};
class Car {
Engine engine; // Car owns its Engine outright; created and destroyed with the Car
public:
void drive() {
engine.start();
cout << "Car is driving." << endl;
}
};
// Aggregation: Student objects exist independently of any one University
class Student {
public:
string name;
};
class University {
vector<Student*> students; // University holds references, doesn't own their lifetime
public:
void enroll(Student* s) { students.push_back(s); }
};
int main() {
Car myCar;
myCar.drive();
Student s{"Alex"};
University u;
u.enroll(&s); // s exists independently before, during, and after this relationship
return 0;
}
Engine is created as a direct member of Car, it's born and destroyed with the Car, that's composition. Student objects are created independently and just referenced by University through a pointer, that's aggregation.
Where this matters in real systems
A widely repeated piece of real design advice is "favor composition over inheritance," and it comes from real, painful experience: deep inheritance hierarchies get brittle fast, a change to a base class three levels up can unexpectedly break behavior in a leaf class nobody was thinking about. Composition avoids that fragility because each piece is independently swappable. This is exactly why modern UI frameworks like React are built around composing small components together rather than building deep class inheritance trees, and why dependency injection in backend frameworks (like Spring in Java) is fundamentally about wiring objects together via composition instead of inheritance.
What interviewers actually ask about composition, aggregation, and inheritance
- "What's the actual difference between composition and aggregation?" Ownership and lifecycle: composition means the contained object's lifetime is tied to the container (it can't outlive it); aggregation means the objects have independent lifetimes.
- "Why is 'favor composition over inheritance' such common advice?" Deep inheritance hierarchies create tight coupling and fragility, changes to a base class can unexpectedly ripple into every subclass. Composition keeps pieces independent and easier to change or swap out.
- "Give a real example of when inheritance is still the right choice." When there's a genuine, stable is-a relationship and you need polymorphism, for example a payment processor interface with distinct subclasses for CreditCardPayment, PayPalPayment, and BankTransferPayment that all need to be usable interchangeably through a common interface.
- "How would you model a 'Car has an Engine' relationship, and why not inheritance?" Composition, because a Car isn't a specialized kind of Engine, it owns one. Modeling it as inheritance (Car extends Engine) would violate the whole point of is-a relationships.
Key takeaways
- Inheritance models is-a; composition and aggregation both model has-a.
- Composition means strong ownership, the contained object's lifetime is tied to its owner.
- Aggregation means loose ownership, the referenced object has an independent lifetime.
- "Favor composition over inheritance" is common, real advice because deep inheritance hierarchies tend to become fragile over time.
More in Inheritance
Browse all OOP topics
Log in to save which topics you've read across every subject.
Frequently asked questions
What's the actual difference between composition and aggregation?
Ownership and lifecycle: composition means the contained object's lifetime is tied to the container (it can't outlive it); aggregation means the objects have independent lifetimes.
Why is 'favor composition over inheritance' such common advice?
Deep inheritance hierarchies create tight coupling and fragility, changes to a base class can unexpectedly ripple into every subclass. Composition keeps pieces independent and easier to change or swap out.
Give a real example of when inheritance is still the right choice.
When there's a genuine, stable is-a relationship and you need polymorphism, for example a payment processor interface with distinct subclasses for CreditCardPayment, PayPalPayment, and BankTransferPayment that all need to be usable interchangeably through a common interface.
How would you model a 'Car has an Engine' relationship, and why not inheritance?
Composition, because a Car isn't a specialized kind of Engine, it owns one. Modeling it as inheritance (Car extends Engine) would violate the whole point of is-a relationships.