LearnViz CompSci
Home / Learn / Programming Fundamentals / Composition over Inheritance

Advanced Topic 46 of 54 Programming Fundamentals

Composition over Inheritance

Preferring building objects from parts to deep class hierarchies.

How it works

Instead of inheriting behavior from a parent class, an object holds other objects and delegates work to them. This lets behavior be swapped at runtime, avoids rigid class trees and combines capabilities freely. Strategy, decorator and dependency-injection patterns are built from composition, and it reflects a has-a relationship.

Step-by-step walkthrough

  1. Instead of Car extending Engine, a Car has an Engine.
  2. It also has wheels and a radio as parts.
  3. Parts can be swapped, for example an electric engine.
  4. Behavior is combined by delegating to parts.
  5. This avoids deep, rigid class trees.

Where it is used

A Car has an Engine rather than being a kind of Engine, a game entity has components for movement and rendering, and a logger composes a formatter and a writer. Composition is common in modern frameworks and entity-component systems.

Common pitfall

Composition adds boilerplate for delegation and more objects to wire together. Inheritance is still fine for genuine is-a relations with stable hierarchies. Avoid deciding by habit: ask whether the relationship is an identity or a role, and whether behavior needs to change at runtime.

Where it fits in the course

Composition over Inheritance is topic 46 of 54 in Programming Fundamentals, rated Advanced. It follows Interfaces and Abstract Classes. The next topic is Functional Programming.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Programming Fundamentals quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Composition over Inheritance?
Preferring building objects from parts to deep class hierarchies. It belongs to Programming Fundamentals and is an advanced-level topic.
Where do people use Composition over Inheritance?
A Car has an Engine rather than being a kind of Engine, a game entity has components for movement and rendering, and a logger composes a formatter and a writer.
What is the most common mistake with Composition over Inheritance?
Composition adds boilerplate for delegation and more objects to wire together.
What should I learn before this topic?
Start with Interfaces and Abstract Classes, the topic just before it in Programming Fundamentals.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Programming Fundamentals quiz.
Is Composition over Inheritance hard to learn?
It is an Advanced topic. Expect to read the explanation more than once and to connect it with earlier topics. Working through a small example by hand helps most.
How long does it take to learn Composition over Inheritance?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Composition over Inheritance?
Interfaces and Abstract Classes, Functional Programming, Abstraction are closely connected. The full list is in the related topics section of this page.
What should I study after Composition over Inheritance?
Continue with Functional Programming, the next topic in Programming Fundamentals.

Related topics