Skip to content
Object-Oriented Programming and Design · Term 2

Class Hierarchies and Inheritance

Design systems using parent and child classes to model real-world relationships and reduce code redundancy.

Need a lesson plan for Computer Science?

Generate Mission

Key Questions

  1. How can we design a class hierarchy that is flexible enough for future requirements?
  2. What are the dangers of deep inheritance trees in large software projects?
  3. How does inheritance promote the principle of DRY (Don't Repeat Yourself)?

Ontario Curriculum Expectations

CS.HS.P.4CS.HS.D.1
Grade: Grade 11
Subject: Computer Science
Unit: Object-Oriented Programming and Design
Period: Term 2

About This Topic

Class hierarchies and inheritance enable students to build efficient object-oriented systems by modeling real-world relationships through parent and child classes. A parent class, such as Vehicle, defines common attributes like speed and methods like accelerate, which child classes like Car and Truck inherit and extend with specifics such as numberOfDoors or loadCapacity. This structure reduces code redundancy, directly embodying the DRY principle, and prepares students for flexible software design in larger projects.

In the Ontario Grade 11 Computer Science curriculum, this topic advances from basic classes to emphasize thoughtful hierarchy design. Students address key questions: How to create hierarchies flexible for future changes? What risks come from deep inheritance trees, like maintenance challenges and fragility? Practical examples, from library book systems to game character families, illustrate how balanced inheritance promotes clean, adaptable code.

Active learning excels with this topic because students construct and test hierarchies collaboratively. Pair programming reveals inheritance errors instantly, group refactoring debates design choices, and iterative builds show DRY benefits in action. These hands-on methods turn abstract concepts into tangible skills, boosting confidence and retention for real-world programming.

Learning Objectives

  • Design a simple class hierarchy to model a collection of related real-world objects, demonstrating inheritance.
  • Analyze existing code to identify opportunities for applying inheritance to reduce redundancy and improve maintainability.
  • Compare and contrast the use of inheritance versus composition in designing software solutions.
  • Evaluate the potential drawbacks of deep inheritance trees, such as increased coupling and reduced flexibility.
  • Create a program that utilizes abstract classes or interfaces to define common behaviors for a hierarchy.

Before You Start

Introduction to Classes and Objects

Why: Students must understand the fundamental concepts of classes as blueprints and objects as instances before they can grasp how classes relate to each other through inheritance.

Methods and Attributes

Why: The core of inheritance involves inheriting methods and attributes, so students need a solid grasp of these concepts within a single class.

Key Vocabulary

InheritanceA mechanism where a new class (child or subclass) derives properties and behaviors from an existing class (parent or superclass).
Superclass (Parent Class)The class whose features are inherited by another class. It represents a more general concept.
Subclass (Child Class)The class that inherits features from a superclass. It represents a more specific concept.
Method OverridingWhen a subclass provides a specific implementation of a method that is already defined in its superclass.
PolymorphismThe ability of an object to take on many forms, often achieved through inheritance, allowing objects of different subclasses to be treated as objects of their common superclass.

Active Learning Ideas

See all activities

Real-World Connections

Video game developers use class hierarchies to model characters, enemies, and items. A base 'Character' class might have attributes like 'health' and 'attackPower', with subclasses like 'Warrior' or 'Mage' inheriting these and adding unique abilities such as 'charge' or 'castSpell'.

Software engineers designing inventory management systems for large retailers, like Walmart, might use inheritance. A base 'Product' class could define common properties like 'price' and 'SKU', while subclasses like 'Electronics' or 'Apparel' add specific attributes like 'warrantyPeriod' or 'sizeChart'.

Watch Out for These Misconceptions

Common MisconceptionInheritance should always be used to share code, even deeply.

What to Teach Instead

Deep trees create tight coupling and hard-to-change code. Group refactoring activities let students experience maintenance pain, then compare with composition alternatives, helping them choose balanced designs through discussion.

Common MisconceptionChild classes inherit all parent members, including private ones.

What to Teach Instead

Only public and protected members inherit; private stay in parent. Hands-on coding pairs where students try accessing private fields reveal errors immediately, prompting talks on access modifiers and encapsulation.

Common MisconceptionEvery child method must override the parent version.

What to Teach Instead

Inheritance uses parent methods by default unless overridden. Testing hierarchies in small groups shows default behavior works for shared logic, reinforcing when overrides add value through real debugging.

Assessment Ideas

Quick Check

Present students with a scenario, e.g., modeling different types of musical instruments. Ask them to identify a potential superclass and two subclasses, listing one unique attribute or method for each subclass. This checks their ability to conceptualize hierarchies.

Discussion Prompt

Pose the question: 'When might it be better to use composition (having an object contain another object) instead of inheritance?' Facilitate a discussion where students articulate the trade-offs, focusing on flexibility and avoiding deep, rigid hierarchies.

Exit Ticket

Give students a small code snippet demonstrating inheritance. Ask them to write down what the output will be and explain, in one sentence, how inheritance made the code shorter than if they had written it without a parent class.

Ready to teach this topic?

Generate a complete, classroom-ready active learning mission in seconds.

Generate a Custom Mission

Frequently Asked Questions

How do class hierarchies promote the DRY principle?
Hierarchies place shared code in parent classes, so children inherit without repetition. For instance, all vehicles share a move method, avoiding duplicate implementations. Students see this in refactoring tasks, where extracting common code cuts lines dramatically and simplifies updates across the system.
What are the risks of deep inheritance trees?
Deep trees lead to fragile base class problems, where parent changes break distant children, and debugging traces long chains. They violate single responsibility by mixing concerns. Classroom design reviews help students spot these early, favoring shallow hierarchies or composition for robust projects.
How can active learning help teach class hierarchies and inheritance?
Active approaches like pair programming and group refactoring make inheritance concrete. Students build hierarchies, encounter errors like method shadowing, and fix them collaboratively. This reveals DRY benefits and deep tree pitfalls through trial and error, far better than lectures, building design intuition for flexible systems.
How to design flexible class hierarchies for future changes?
Favor shallow depths, use interfaces for behaviors, and apply composition over deep inheritance. Start with broad parents, test extensions early. Student-led hierarchy expansions in projects show how initial choices affect adaptability, teaching iterative design aligned with software best practices.