Introduction

When a class inherits from another, it doesn’t have to behave exactly the same.

A child class can:

  • Override methods from the parent class
  • Change how inherited behaviour works
  • Extend parent behaviour using super()

This allows programs to stay organised while still being flexible and powerful.

In this lesson, you will learn how to:

  • Override a method in a child class
  • Use super() to reuse parent behaviour
  • Understand when and why overriding is useful

Overriding a Method

A method is overridden when a child class defines a method with the same name as one in the parent.

Python will always use the child version if it exists.

class Parent:
    def greet(self):
        print("Hello from parent")

class Child(Parent):
    def greet(self):
        print("Hello from child")

Predict:

What do you think will be printed?

class Animal:
    def speak(self):
        print("The animal makes a sound")

class Dog(Animal):
    def speak(self):
        print("The dog barks")

dog = Dog()
dog.speak()

Think about:

  • Both classes define speak()
  • Which one will Python choose?

Run:

Run the program below to check your prediction.

Why Use super()?

Sometimes you don’t want to completely replace the parent method — you just want to extend it.

This is where super() is used.

super().method_name()

It calls the version of the method from the parent class.

Investigate:

In this example, the child class adds extra behaviour while keeping the parent behaviour.

Try this:

  • Remove the line with super()
  • Run the code again
  • What changes?

Modify:

The following program attempts to extend a parent method but contains an error.

Fix the code so that both messages are printed.

The problem

  • super() is not being called correctly

Hint:

super().method_name()
Scroll to Top