What is Inheritance?

Reuse code from existing classes instead of writing everything from scratch.

Introduction

Inheritance lets a class reuse code from another class. Instead of rewriting the same attributes and methods, you extend an existing class. The new class automatically gets everything the original class has, and you can add more on top. This is one of the most powerful ideas in object-oriented programming.

The Concept

Think of inheritance like a family tree. A child inherits traits from their parent -- eye color, height, and so on. In programming, a child class inherits attributes and methods from a parent class. The child gets everything the parent has, plus it can have its own unique features.

Key idea: Inheritance creates an "is-a" relationship. A Dog is an Animal. A Car is a Vehicle. A Manager is an Employee.

Why Inheritance?

  • Avoid code duplication: Write shared logic once in the parent, reuse it in every child.
  • Build on existing work: Extend a class without modifying it.
  • Model real-world hierarchies: Animal to Dog, Vehicle to Car, Employee to Manager.

Basic Syntax

To inherit from a class, put the parent class name in parentheses after the child class name. The child class automatically gets all of the parent's attributes and methods.

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(f"{self.name} makes a sound")

class Dog(Animal):
    pass

d = Dog("Buddy")
d.speak()  # Buddy makes a sound -- inherited!

The Dog class has no code of its own -- just pass. Yet it can use __init__ and speak() because it inherited them from Animal.

OOP Terminology

  • Parent / Base / Super class: The class being inherited from (e.g., Animal).
  • Child / Derived / Sub class: The class that inherits (e.g., Dog).
  • Inheritance: Creating a new class from an existing class, reusing its code.

Real-World Connection

Inheritance mirrors how we categorize things in the real world:

  • Employee to Manager -- a manager is an employee with extra responsibilities.
  • Vehicle to Car to ElectricCar -- each level adds more specifics.
  • Shape to Circle, Rectangle -- different shapes share common traits like color.

Best Practices

  • Use inheritance when there is a clear "is-a" relationship between classes.
  • Keep parent classes general and child classes specific.
  • Do not inherit just to reuse a single method -- that is usually a sign to use composition instead.

Common Mistakes

  • Forgetting the parentheses: class Dog(Animal) inherits, but class Dog does not.
  • Using inheritance when there is no real "is-a" relationship -- a Kitchen is not a Microwave.
  • Creating overly deep inheritance chains that become hard to follow and maintain.

Practice Exercises

  1. Create a Vehicle class with a brand attribute and a honk() method. Then create a Car class that inherits from it and test calling honk() on a Car object.
  2. Create a Person class with name and age. Create a Student class that inherits from Person using pass. Verify the student can access name and age.
  3. Create a Phone class with a call() method. Create Smartphone that inherits from Phone. Does the smartphone have the call method without writing any extra code?

Summary

  • Inheritance lets a child class reuse all the code from a parent class.
  • Use parentheses to specify the parent: class Child(Parent).
  • The child automatically gets all parent attributes and methods.
  • Inheritance models "is-a" relationships and reduces code duplication.