Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
← Fundamentals of Object Oriented Programming Level Two Β· Lesson 7 of 8

Module Six

πŸ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
7
Module 6: Composition, Aggregation & Relationships for Young Coders
⭐ Module 6 · Level Two

Composition, Aggregation
& Relationships

🧩 How objects connect, share, and live together

🎯 What you'll learn in this module

  • βœ” The difference between association, aggregation, and composition
  • βœ” How to design relationships between your objects
  • βœ” How to avoid tight coupling (the sneaky enemy of clean code)
  • βœ” The basics of dependency direction and layering
  • βœ” When to use inheritance vs. composition
  • βœ” How to model real‑world relationships cleanly
πŸ”—
Objects Connect β€” But How?
The Big Idea

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.

πŸ”Œ A lamp has a light bulb (composition). But a person has a favourite colour (a simple property). Different kinds of "has," different strengths.
🀝
Association: "Knows About"
Relationship

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.

πŸ‘₯ Friends who hang out: they share moments, but neither owns the other. If one moves away, the other keeps going.
🧺
Aggregation: "Has Many" (But Loosely)
Relationship

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.

🎡 A playlist holds songs. Delete the playlist β€” the songs are still in your library. The container was temporary.
🏠
Composition: "Owns" (Life or Death)
Relationship

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.

πŸ«€ Your body "has" a heart. You can't delete your heart and still be you. Same with a car and its engine β€” one without the other isn't the same object.
πŸ“Š
Side‑by‑Side: Which One?
Compare
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
πŸ‘¨β€πŸ« Teacher
↔
πŸ‘¦ Student
Association
🎡 Playlist
◇──
🎢 Song
Aggregation
🏠 House
◆──
πŸ›‹οΈ Room
Composition
🎨 Association is friendship. Aggregation is a book club. Composition is a sandwich β€” the bread and filling are one thing together.
πŸ”—
Tight Coupling: The Sneaky Enemy
Warning

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.

🧡 Tight coupling is two shirts sewn together β€” pull one thread and both fall apart. Loose coupling is a shirt with a detachable collar β€” the shirt works either way.
🧭
Dependency Direction & Layering
Design

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.

🎨 UI Layer
↓ depends on
🧠 Business Logic
↓ depends on
πŸ’Ύ Data / Storage
πŸ›οΈ In a building, the top floors rest on the lower ones. If the lower floor was built on the top floor, the whole building would collapse.
βš–οΈ
Inheritance vs. Composition: Which Wins?
Wisdom

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.

πŸ” A burger has a bun, patty, and sauce. You wouldn't invent a "BurgerWithCheeseAndLettuce" subclass each time β€” you just add layers. Composition is like adding layers.
⚠️ Common trap: Beginners often default to inheritance because it feels powerful. But deep inheritance trees cause tight coupling and rigid code. If you find yourself saying "my class is a..." and it feels forced, try composition β€” "my class has a..." is usually more flexible.

πŸ› οΈ Hands-on activities (no boring stuff!)

1

Relationship detective: For each pair, decide: association, aggregation, or composition? (a) Author ↔ Book, (b) Library β†’ Book, (c) Chapter β†’ Page. Write one sentence justifying each.

2

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.

3

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.

4

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.

5

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.

6

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.

πŸ† Get Certified

πŸ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (₦2,000 or 20,000 coins), and get a verified certificate with your name on it.

πŸŽ“ Sign Up to Get Certified
πŸ› οΈ Practice Tools
Hands-on simulators & labs - subscription required.
β†’
🎯 Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
β†’