---
title: "Inheritance and Overriding Behaviour"
description: "A subclass reuses a superclass's methods and can extend its initialization or replace methods. Instance fields still need initialization. Shared behaviour is written once, with each difference in the "
canonical: https://lightmysky.com/learn/computing/inheritance-and-overriding-behaviour-mt_wBp7j_B6fj
source: https://lightmysky.com/learn/computing/inheritance-and-overriding-behaviour-mt_wBp7j_B6fj.md
retrieved: 2026-11-12
---

> **Agent view.** This is the Markdown twin of the page, for tools and assistants.
> When to use this site, and the call that answers each job: https://lightmysky.com/agent-instructions.md
> API description (OpenAPI 3.1): https://lightmysky.com/openapi.json · Authentication: https://lightmysky.com/auth.md
> Pricing: https://lightmysky.com/pricing.md · Catalog: https://lightmysky.com/llms.txt · Full catalog: https://lightmysky.com/llms-full.txt
> Every machine-readable file on this domain: https://lightmysky.com/.well-known/ai-catalog.json
> Ask for Markdown with `Accept: text/markdown`, a `.md` address, or `?mode=agent`.

# Inheritance and Overriding Behaviour

A subclass reuses a superclass's methods and can extend its initialization or replace methods. Instance fields still need initialization. Shared behaviour is written once, with each difference in the class responsible for it.

Subject: Computing · Area: Programming · Ages 16 to 18
Page: https://lightmysky.com/learn/computing/inheritance-and-overriding-behaviour-mt_wBp7j_B6fj

## Ready when they can

- Write a subclass that adds one field and overrides one method
- Predict which version of a method runs for an object of the subclass
- Say when two classes should share a parent and when they should not

## Lesson: Borrow the shared parts, rewrite your own

class Child(Parent) makes Child a subclass of superclass Parent, with inherited methods. Instance fields need initialization. Replacing __init__ does not automatically call the parent's initializer.
```python
class Vehicle:
    def __init__(self):
        self.wheels = 4
    def refuel(self):
        return "fuel added"
    def trip_info(self):
        return "vehicle"
```

**Example.** Overriding defines a subclass version of an inherited method. It does not delete the parent's version.
```python
class Shuttle(Vehicle):
    def trip_info(self):
        return "shuttle"
```
Calling trip_info on a Shuttle uses this version; calling it on a Vehicle uses Vehicle's version.

*(drawing: Shuttle's override handles trip_info. Lookup reaches Vehicle for refuel, which Shuttle does not override.)*

super() returns a proxy for inherited lookup. It does not run trip_info by itself. In these examples, super().trip_info() selects and calls Vehicle's version. An override can use its returned text, then add seats information. Calling self.trip_info() inside that same override calls the override again.

**Example.** This subclass initializes the parent fields, adds an instance field and overrides trip_info. str converts the seat count to text.
```python
class Bus(Vehicle):
    def __init__(self, seats):
        super().__init__()
        self.seats = seats
    def trip_info(self):
        return super().trip_info() + ": " + str(self.seats)

bus = Bus(12)
```

Every subclass must honour its parent's behavioural contract. PersistentStore.save(data) might promise that the saved data can be loaded after the process ends. A memory-only cache cannot meet that promise. Reusing a formatting method alone does not make it a valid PersistentStore child.

Sibling classes need not be kinds of one another; each must fit their common parent. Document includes read-only documents. Put replace_text(), promising a content change, on EditableDocument. Returning success without changing content breaks that promise. Polymorphism means different objects can respond to one method name.

**Recap.** Initialize required fields, look up each method separately, and call inherited methods through super().method(). Every child must honour its parent's contract.

*Inheritance teaching adapted from the Introduction to Python Programming chapter 13 summary, with independently written Python examples, contract explanations and assessment repairs.*

## Practice

8 questions on this page, each with its working shown.

## Needs first

- [Classes and Objects: Bundling Data with Behaviour](https://lightmysky.com/learn/computing/classes-and-objects-bundling-data-with-behaviour-mt_kRvg7LZ_kk)

## Opens up

- [Design patterns: naming a solution that keeps recurring](https://lightmysky.com/learn/computing/design-patterns-naming-a-solution-that-keeps-recurring-mt_bhUEk8PwBp)
- [Designing with Objects: Deciding What Each Class Owns](https://lightmysky.com/learn/computing/designing-with-objects-deciding-what-each-class-owns-mt_De_gpvpOzg)
