---
title: "Inheritance and Overriding Behaviour"
description: "A class can extend another, taking its fields and methods and replacing the ones it needs to do differently. Shared behaviour is written once, and the differences sit in the class they belong to."
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-09-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 class can extend another, taking its fields and methods and replacing the ones it needs to do differently. Shared behaviour is written once, and the differences sit in the class they belong to.

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

A subclass is a new class that borrows everything from another class, called its superclass. A SportsCar class can inherit from a general Car class, picking up wheels and an engine without rewriting them. Shared behaviour is written once, and each difference sits in the class it belongs to.

**Example.** Take class Vehicle with a method trip_info, then class Bus that extends Vehicle, adds seats, and writes its own trip_info. A Bus object calling trip_info runs the Bus version, because the subclass version replaces the parent one for its objects. That replacement is called overriding.

A subclass may use an inherited method exactly as it is when nothing needs to change. When an override still needs the old behaviour, it calls the parent version with super() and builds on top instead of throwing it away. Calling the same method name on different classes and getting each own answer is called polymorphism.

**Tip.** Share a parent only when each child truly is one of the parent kind. Bus and Car both are Vehicles, so the parent earns its place. When two classes merely look alike but neither is the other, keep them separate instead of forcing a family.

**Recap.** Inherit what is shared, override what differs, and only join classes that truly share a kind.

## 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)
