Computer Science
Grade 9
20 min
Inheritance Deep Dive
Inheritance Deep Dive
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define inheritance and explain its role in code reuse.
Identify and differentiate between a parent class (superclass) and a child class (subclass).
Create a child class that inherits attributes and methods from a parent class.
Use the 'super()' keyword to call a parent class's constructor and methods.
Override a parent class's method in a child class to provide specialized functionality.
Explain the 'is-a' relationship in the context of inheritance.
Have you ever noticed how you inherit traits like eye color from your parents? 🧬 In programming, we can do the same thing with code!
This lesson explores inheritance, a core principle of Object-Oriented Programming. You will learn how to create new classes that reuse, extend, an...
2
Key Concepts & Vocabulary
TermDefinitionExample
InheritanceThe mechanism where one class (the child) acquires the properties (attributes) and behaviors (methods) of another class (the parent). It represents an 'is-a' relationship.A 'Dog' class can inherit from an 'Animal' class. A Dog 'is-a' type of Animal.
Parent Class (Superclass)The class whose properties and methods are inherited by another class. It is the more general class.In the relationship between 'Animal' and 'Dog', the 'Animal' class is the Parent Class.
Child Class (Subclass)The class that inherits from another class. It is the more specific class and can add its own unique properties and methods.In the relationship between 'Animal' and 'Dog', the 'Dog'...
3
Core Syntax & Patterns
Creating a Child Class
class ChildClassName(ParentClassName):
# class content here
To make one class inherit from another, place the parent class's name in parentheses after the child class's name during its definition.
Calling the Parent Constructor
def __init__(self, params...):
super().__init__(parent_params...)
Inside the child class's constructor (`__init__`), use `super().__init__()` to run the parent's constructor. This ensures that all the parent's attributes are properly initialized before adding the child's specific attributes.
Overriding a Method
class Child(Parent):
def method_name(self, params...):
# new implementation
To override a method, simply define a method in the child class with the exact same na...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You are designing classes for a game: a parent class `Enemy` and child classes `Goblin` and `Dragon`. All enemies have a `health` attribute, but Goblins also have a `tribe_name` and Dragons have `fire_breath_damage`. What is the most effective way to design their `__init__` methods using inheritance?
A.Each class should have a completely separate `__init__` method that initializes all attributes from scratch.
B.The `Enemy` `__init__` should handle `health`. `Goblin` and `Dragon` should call `super().__init__()` and then initialize their own unique attributes.
C.The `Enemy` `__init__` should handle all possible attributes (`health`, `tribe_name`, `fire_breath_damage`) and the child classes just call it.
D.Only the child classes should have `__init__` methods; the parent class does not need one.
Challenging
A `Character` class has an `attack(target)` method that reduces the target's health. A `Wizard` subclass needs its `attack(target)` to do the same damage, but also reduce its own `mana` by 5. What is the most efficient and maintainable way to implement the `Wizard`'s `attack` method?
A.Copy the health-reduction code from `Character.attack` into `Wizard.attack` and then add the line `self.mana -= 5`.
B.In `Wizard.attack`, call `super().attack(target)` to handle the health reduction, and then add the line `self.mana -= 5`.
C.In `Wizard.attack`, only include the line `self.mana -= 5` and assume the health reduction happens automatically.
D.Create a new method in `Wizard` called `cast_spell` and do not override `attack`.
Challenging
Consider a hierarchy: `Person` is the parent, `Student` and `Professor` are children. The `Person` class `display` method prints the person's name. The `Student` and `Professor` `display` methods need to print the name AND their specific information (GPA for Student, salary for Professor). How does using `super().display()` in the child classes improve the design?
A.It makes the program run faster.
B.It's the only way for the child methods to access the `name` attribute.
C.It centralizes the logic for displaying a person's name. If the format for displaying the name changes, you only need to edit the `Person` class.
D.It prevents the `Student` and `Professor` classes from needing their own `display` methods.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free