OOP – Inheritance (basic), online

Six activities your students do on their own devices. See who's finished, and their scores.

OOP – Inheritance (basic) in one minute

What students need to know

Dog gets all of Animal's methods and attributes automatically. super().__init__() sets up the parent's attributes in the child.

Inheritance lets a child class reuse all the code already written in the parent. The class in parentheses is the parent — Dog inherits from Animal.

Mistakes students make

  • Should call super().__init__(name) to use the parent's initialization.
  • Parent's __init__ expects name must pass it: super().__init__(name).
  • Python uses parentheses for inheritance, not 'extends' (that's Java).
  • super needs parentheses: super().__init__, not super.__init__.

Discover other journeys with this module

What students will learn in this module

Every question in Module 56

Quick questions

  1. What is inheritance?AA child class gets methods from a parent classBCopying a file
  2. class Dog(Animal): what is Animal?AThe parent (base) classBThe child class
  3. What does super() do?ACreates a new classBAccesses the parent class
  4. If Animal has speak(), does Dog get it?ANo must redefineBYes automatically through inheritance
  5. Can Dog override Animal's speak()?AYes define speak() in DogBNo parent methods are locked

Key terms

Students type the meaning of:

  • Inheritance
  • Parent class
  • Child class
  • super()

Match the pairs

class Dog(Animal)Match it to what it does
super(Animal).__init__()Match it to what it does
class Dog(Animal):Match it to what it does
super().__init__()Match it to what it does
issubclass(bool, int)Match it to what it does
isinstance(True, int)Match it to what it does

Fill in the blanks

  1. class Dog(______):
        pass
  2. class Dog(Animal):
        def ________(self, name, breed):
            _____().________(name)
  3. class Dog(Animal):
        def speak(self):
            return self.name + ' says Woof!'
    # this _________ the parent
  4. dog = Dog('Rex')
    print(__________(dog, Animal))
  5. class Circle(Shape):
        def ________(self, color, radius):
            _____().________(color)

Spot the error

Question 1

Aclass Dog(Animal):B    def __init__(self, name, breed):C        self.name = nameD        self.breed = breed

Question 2

Aclass Dog(Animal):B    def __init__(self, name, breed):C        super().__init__()D        self.breed = breed

Question 3

Aclass Dog extends Animal:B    def __init__(self, name):C        super().__init__(name)D    def bark(self):E        return 'Woof!'

Question 4

Aclass Dog(Animal):B    def __init__(self, name):C        super.__init__(name)

Question 5

Aclass Animal:B    def speak(self):C        return 'Sound'DEclass Dog(Animal):F    def speak():G        return 'Woof'