Composition vs. InheritanceActivities & Teaching Strategies
Active learning helps students grasp composition versus inheritance by doing rather than listening. Programming challenges make abstract relationships concrete, while discussions expose reasoning gaps in real time. This approach builds intuition for trade-offs in flexibility, coupling, and maintainability that lectures alone cannot convey.
Learning Objectives
- 1Compare and contrast the 'is-a' and 'has-a' relationships represented by inheritance and composition, respectively.
- 2Analyze the advantages of using composition over inheritance for enhancing code flexibility and maintainability in object-oriented designs.
- 3Justify the selection of either composition or inheritance for a given software design problem, citing specific reasons.
- 4Critique existing code examples to identify instances where composition could improve upon an inheritance-based design.
Want a complete lesson plan with these objectives? Generate a Mission →
Pair Programming: Vehicle Design Challenge
Pairs sketch 'is-a' and 'has-a' relationships for vehicles, engines, and wheels. They code both inheritance and composition versions, then run tests to swap engine types. Discuss which approach allows easier changes.
Prepare & details
Differentiate between 'is-a' and 'has-a' relationships in object-oriented design.
Facilitation Tip: During the Pair Programming: Vehicle Design Challenge, ask pairs to swap partners after the first 20 minutes to expose them to different design approaches.
Setup: Two teams facing each other, audience seating for the rest
Materials: Debate proposition card, Research brief for each side, Judging rubric for audience, Timer
Small Groups: Refactoring Relay
Groups receive inheritance-based code for a library system. Each member refactors one class to composition, passes to the next, and notes changes in flexibility. Debrief as a class on outcomes.
Prepare & details
Analyze the advantages of composition over inheritance in terms of flexibility and maintainability.
Facilitation Tip: In Small Groups: Refactoring Relay, provide starter code with deep inheritance and require groups to submit a flattened version before adding new features.
Setup: Two teams facing each other, audience seating for the rest
Materials: Debate proposition card, Research brief for each side, Judging rubric for audience, Timer
Whole Class: Design Debate
Divide class into teams: one defends inheritance, the other composition for a game character system. Teams code minimal examples, present pros/cons with live demos, and vote on best fit.
Prepare & details
Justify the choice between composition and inheritance for a given design problem.
Facilitation Tip: During Whole Class: Design Debate, assign roles like 'inheritance advocate' or 'composition advocate' to ensure balanced participation.
Setup: Two teams facing each other, audience seating for the rest
Materials: Debate proposition card, Research brief for each side, Judging rubric for audience, Timer
Individual: Choice Justification
Students select a personal project, implement using one principle, then refactor to the other. Write a short report justifying the final choice based on maintainability and flexibility.
Prepare & details
Differentiate between 'is-a' and 'has-a' relationships in object-oriented design.
Facilitation Tip: For Individual: Choice Justification, require students to submit both a code snippet and a 2-sentence rationale to reveal gaps between design and explanation.
Setup: Two teams facing each other, audience seating for the rest
Materials: Debate proposition card, Research brief for each side, Judging rubric for audience, Timer
Teaching This Topic
Experienced teachers introduce this topic by modeling flawed designs first, letting students experience the pain of change. Research shows that students overuse inheritance until they feel its brittleness firsthand. Avoid teaching composition as 'inheritance but different'—instead focus on modeling relationships students recognize from everyday objects like cars or computers.
What to Expect
Successful learning looks like students confidently explaining why a design uses composition or inheritance and backing it with code examples. They should also critique designs without defaulting to inheritance and justify choices based on changeability. Peer reviews and refactoring outputs show clear evidence of this understanding.
These activities are a starting point. A full mission is the experience.
- Complete facilitation script with teacher dialogue
- Printable student materials, ready for class
- Differentiation strategies for every learner
Watch Out for These Misconceptions
Common MisconceptionDuring Pair Programming: Vehicle Design Challenge, watch for students defaulting to inheritance for code reuse without considering delegation.
What to Teach Instead
Prompt pairs to ask, 'Can this part change independently of the whole?' and require them to sketch a composition alternative before coding.
Common MisconceptionDuring Small Groups: Refactoring Relay, watch for students flattening hierarchies without testing substitutability.
What to Teach Instead
Require groups to write unit tests that verify subclass instances can replace superclass instances before submitting their refactored code.
Common MisconceptionDuring Whole Class: Design Debate, watch for students conflating 'is-a' with 'has-a' when justifying choices.
What to Teach Instead
Ask groups to map their designs to UML and label each relationship as 'is-a' or 'has-a' before presenting, ensuring clarity in their arguments.
Assessment Ideas
After Small Groups: Refactoring Relay, present students with two class diagrams: one using inheritance (e.g., 'Dog' inherits from 'Animal') and another using composition (e.g., 'Car' has an 'Engine'). Ask students to write one sentence for each diagram explaining the relationship modeled and identify which uses 'is-a' and which uses 'has-a'.
During Pair Programming: Vehicle Design Challenge, pose the scenario: 'Imagine you are designing a system for different types of electronic devices (e.g., smartphone, tablet, laptop). Discuss with a partner whether you would primarily use inheritance or composition to model the shared functionalities like 'power on/off' and 'display screen'. Justify your choice.' Listen for reasoning tied to changeability and coupling.
After Individual: Choice Justification, ask students to write down one situation where inheritance would be a suitable design choice and one situation where composition would be preferable. For each, briefly explain why their chosen principle fits the scenario.
Extensions & Scaffolding
- Challenge students to design a system for a music streaming service where both inheritance and composition appear intentionally, then write a paragraph comparing their trade-offs.
- Scaffolding: Provide a partially completed UML diagram for the Refactoring Relay to help students visualize the starting hierarchy.
- Deeper exploration: Have students research the 'Hollywood Principle' and rewrite their refactored code to explicitly use it.
Key Vocabulary
| Inheritance | A mechanism where a new class (subclass or derived class) acquires properties and behaviors from an existing class (superclass or base class). It models an 'is-a' relationship. |
| Composition | A design principle where a class contains instances of other classes as members, allowing it to use their functionality. It models a 'has-a' relationship. |
| 'is-a' relationship | A type of relationship in object-oriented design where one class is a specialized version of another class, inheriting its characteristics. For example, a 'Car' is-a 'Vehicle'. |
| 'has-a' relationship | A type of relationship in object-oriented design where one class contains an instance of another class as a component, utilizing its features. For example, a 'Car' has-a 'Engine'. |
| Code Reuse | The practice of using existing code in new programs to reduce development time and effort, achieved through mechanisms like inheritance and composition. |
Suggested Methodologies
More in Object-Oriented Programming and Design
Introduction to Object-Oriented Programming (OOP)
Students will understand the fundamental concepts of OOP: objects, classes, and instances, and their role in modeling real-world entities.
2 methodologies
Encapsulation and Data Privacy
Implement access modifiers to protect internal object states and ensure data integrity.
2 methodologies
Class Hierarchies and Inheritance
Design systems using parent and child classes to model real-world relationships and reduce code redundancy.
2 methodologies
Polymorphism and Interfaces
Utilize interfaces and abstract classes to define common behaviors across different object types.
2 methodologies
Abstract Classes and Methods
Students will learn to use abstract classes to define common interfaces for a group of related classes, enforcing specific behaviors.
2 methodologies
Ready to teach Composition vs. Inheritance?
Generate a full mission with everything you need
Generate a Mission