So far, we've built objects. But real programs are made of objects that know about each other. A School contains Students. A Car has an Engine. A Player owns a Weapon.
The way objects connect matters a lot. Some connections are tight β a car can't exist without its engine. Others are loose β a player can swap weapons. Picking the right connection is half of good design.
Association is the loosest kind of connection: two objects "know about" each other. A Teacher knows their Students. A Doctor knows their Patients.
Neither owns the other. If the teacher retires, the students still exist. If a student graduates, the teacher still exists. They're independent β they just happen to cross paths.
Aggregation is a stronger relationship: one object "has many" of another. A Classroom has many Students. A Playlist has many Songs.
But critically: the parts can exist independently. If the classroom is demolished, the students still exist. If the playlist is deleted, the songs still exist in your library. The container holds the parts, but doesn't own their existence.
Composition is the strongest relationship: one object owns another. A Car owns its Engine. A House owns its Rooms. If the container is destroyed, the parts go with it.
You can't have a working car without an engine, and a house without rooms isn't really a house. The parts are created together and destroyed together.
| Relationship | Meaning | Can Parts Exist Alone? | Example |
|---|---|---|---|
| Association | Knows about | Yes β totally independent | Teacher β Student |
| Aggregation | Has many (loose) | Yes β parts live without container | Playlist β Song |
| Composition | Owns (strong) | No β parts die with container | House β Room |
Coupling is how much one class depends on another. Tight coupling means: change one, and you break the other. That's dangerous β small edits can cascade into huge bugs.
Loose coupling is the goal. Two classes can work together without being deeply tangled. If one changes, the other barely notices. In general, prefer association and aggregation over composition β unless the "owns" relationship is truly essential.
Bigger apps are built in layers: UI on top, business logic in the middle, storage at the bottom. The most important rule: depend downward, not upward. Business logic shouldn't know about the UI.
Why? Because then you can swap the UI (web β mobile β console) without touching your core logic. Dependency direction is how clean code stays clean as projects grow.
When should a class inherit, and when should it contain other objects? A quick rule: prefer composition unless inheritance is truly the right fit.
Inheritance says "is a" (a Dog is an Animal). Composition says "has a" (a Car has an Engine). If you find yourself writing five subclasses just to reuse a small feature, composition would likely be cleaner β and more flexible later.
Relationship detective: For each pair, decide: association, aggregation, or composition? (a) Author β Book, (b) Library β Book, (c) Chapter β Page. Write one sentence justifying each.
Playlist aggregation: Build a Song class and a Playlist class that holds a list of songs. Delete the playlist, then confirm the songs still exist in a separate variable β that's aggregation.
Car composition: Build a Car class that creates its Engine internally. When the Car is destroyed, the Engine goes with it. Show that the engine can't be "moved" to another car in your design.
Coupling check: Write two classes that are tightly coupled (class A directly creates and uses B with hard-coded assumptions). Then refactor them to use an interface so they're loosely coupled. Compare the two versions.
Layered design: Build a tiny app with three classes: CalculatorUI, CalculatorLogic, HistoryStore. Make sure UI depends on Logic, which depends on Storage β not the other way around.
Inheritance vs composition: Build a Bird class two ways. First: FlyingBird extends Bird. Second: Bird has a FlightBehaviour. Then add a Penguin that can't fly. Which design is easier to extend? Explain.
By the end of this module, you'll be able to: distinguish association, aggregation, and composition, choose the right relationship for your design, keep coupling loose, respect dependency direction, and prefer composition over deep inheritance.