Computer Science Grade 9 20 min

Inheritance: Building Upon Existing Classes

Learn about inheritance and how to create subclasses that inherit from parent classes.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define inheritance and explain its role in code reuse. Identify and differentiate between a base class (parent) and a derived class (child). Create a new class that inherits attributes and methods from an existing class. Explain the 'is-a' relationship in the context of inheritance. Override a method from a parent class in a child class to provide specialized functionality. Use the `super()` keyword to call a method from the parent class within the child class. Have you ever noticed how a car, a truck, and a bus are all types of vehicles? 🚌 They share common features like wheels and an engine, but each has its own special purpose. What if we could code that way? In this lesson, you will learn about inheritance, a powerful feature of Object-Ori...
2

Key Concepts & Vocabulary

TermDefinitionExample InheritanceThe process where one class (the child) takes on the attributes and methods of another class (the parent). It's a way to reuse code and establish a relationship between classes.A `Poodle` class can inherit from a `Dog` class. The `Poodle` automatically gets the `Dog`'s attributes (like `name`, `age`) and methods (like `bark()`), but can also have its own unique ones (like `get_haircut()`). Base Class (Parent/Superclass)The class whose features are inherited. It's the more general, or 'parent', class.In the relationship between `Vehicle` and `Car`, the `Vehicle` class is the base class because it represents a more general concept. Derived Class (Child/Subclass)The class that inherits from another class. It's the more specific,...
3

Core Syntax & Patterns

Inheritance Syntax class DerivedClass(BaseClass): # ... new attributes and methods here To make one class inherit from another, you place the name of the base class in parentheses after the derived class's name during its definition. This immediately gives the derived class access to the base class's public attributes and methods. Calling the Parent Constructor class DerivedClass(BaseClass): def __init__(self, param1, param2): super().__init__(param1) # Call parent's init # ... initialize derived class specific attributes Inside the child class's constructor (`__init__`), you must explicitly call the parent class's constructor using `super().__init__()`. This ensures that all the attributes from the parent class are properly i...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You are designing classes for a zoo. You have `Animal`, `Mammal`, and `Lion`. A `Lion` is a type of `Mammal`, and a `Mammal` is a type of `Animal`. How would you structure the `__init__` call in the `Lion` class to ensure all attributes are initialized correctly?
A.The `Lion`'s `__init__` should call `super().__init__()`, which will call the `Mammal`'s `__init__`.
B.The `Lion`'s `__init__` must call both `Animal.__init__()` and `Mammal.__init__()`.
C.The `Lion`'s `__init__` should only initialize `Lion`-specific attributes and ignore the others.
D.The `Lion`'s `__init__` should call `super().__init__()`, which will call the `Animal`'s `__init__` directly, skipping `Mammal`.
Challenging
A `Shape` base class has a method `get_area()` that is not implemented (e.g., it just `pass`es). You create two derived classes, `Circle` and `Square`, that inherit from `Shape`. What is the most critical step to ensure that calling `get_area()` on `Circle` and `Square` objects provides a meaningful result?
A.Rename the method to `get_circle_area()` and `get_square_area()` in the child classes.
B.Each derived class must override the `get_area()` method with its own specific formula for calculating area.
C.Remove the `get_area()` method from the `Shape` class entirely.
D.Add attributes `circle_area` and `square_area` to the `Shape` class.
Challenging
A fellow student has designed a `Car` class that inherits from an `Engine` class. Based on the 'is-a' vs. 'has-a' principle, why is this a fundamentally flawed design?
A.It's not flawed; a car is a type of engine.
B.It's flawed because a `Car` 'is-not-a' `Engine`. A `Car` 'has-an' `Engine`, so composition should be used instead of inheritance.
C.It's flawed because the `Engine` class should inherit from the `Car` class.
D.It's flawed because classes cannot model physical objects.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Object-Oriented Programming: Designing with Classes and Objects

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.