Inheritance: Reuse and Extend Class Behavior

Introduction

In this chapter, you will learn inheritance in Python, a key concept in object-oriented programming. Inheritance lets one class reuse and extend another class's attributes and methods. This helps you reduce duplicate code and build cleaner class hierarchies.

Prerequisites

  • Python 3.10+ installed
  • Basic understanding of classes, objects, methods, and constructors
  • Ability to run .py files in terminal or IDE

What Is Inheritance

Inheritance means a child class can inherit features from a parent class.

Think of it like:

  • Parent class: general template
  • Child class: specialized version with extra behavior

Benefits:

  • code reuse
  • clearer structure
  • easier maintenance

1) Basic Inheritance Syntax

Use parentheses after child class name to specify parent class.

Dog can use both its own methods and inherited methods.

2) Inheriting Constructor with super()

Use super() to call parent constructor.

super() avoids repeating parent initialization logic.

3) Method Overriding

Child classes can redefine parent methods.

Tip

Best Practice

When overriding, keep method names meaningful and behavior consistent with parent intent.

4) Call Parent Method from Child

Sometimes you want to extend, not fully replace, parent behavior.

Output includes both parent and child messages.

5) Real Mini Example: School Roles

This example models roles using inheritance.

This is a practical way to model shared and specialized behavior.

6) Multiple Inheritance (Beginner Awareness)

Python supports multiple inheritance, but beginners should use it carefully.

Warning

Multiple inheritance can become complex in large projects.
Prefer single inheritance unless there is a clear design reason.

Common Beginner Mistakes

Mistake 1: Forgetting super().__init__()

If child class defines its own constructor and skips parent constructor call, parent attributes may be missing.

Mistake 2: Overriding Without Understanding Parent Contract

Changing method behavior too much can break expected usage patterns.

Mistake 3: Creating Deep Inheritance Chains Too Early

Very deep hierarchies are hard to understand and maintain for beginners.

Surprise Practice Challenge

Build a "Vehicle Family" mini design:

  1. Parent class Vehicle with brand and method start()
  2. Child class Car with extra attribute doors
  3. Child class Bike with extra attribute has_pedals
  4. Override start() in at least one child class
  5. Create objects and print behavior

If you finish this, you already understand practical inheritance design basics.

FAQ

Why use inheritance instead of copying code?

Inheritance centralizes shared logic and reduces duplication.

What is the difference between inheritance and composition?

Inheritance models an "is-a" relationship; composition models a "has-a" relationship.

Should I always use inheritance in OOP?

No. Use inheritance when there is a clear shared abstraction; otherwise composition may be simpler.

Is method overriding required in child classes?

No. Child classes can use parent methods directly unless customization is needed.