← Back to Chapters

Python Inheritance

? Python Inheritance

⚡ Quick Overview

Inheritance lets one class (the child or subclass) reuse and extend the behavior of another class (the parent or base class). This helps you:

  • Promote code reusability by sharing common logic in a base class.
  • Create hierarchical relationships like Animal → Dog → Puppy.
  • Customize behavior using method overriding and super().

? Key Concepts

  • Parent/Base class – class whose features are inherited (e.g., Animal).
  • Child/Derived class – class that inherits from the parent (e.g., Dog(Animal)).
  • Single inheritance – child inherits from one parent.
  • Multiple inheritance – child inherits from more than one parent class.
  • Method overriding – child class provides its own version of a parent method.
  • super() – used to call parent methods/constructors from the child class.
  • MRO (Method Resolution Order) – the order in which Python searches classes in multiple inheritance.

? Syntax and Theory

Basic inheritance syntax in Python:

class ChildClass(ParentClass):
The child class name is followed by the parent class in parentheses.

With constructor and super():

super().__init__(args) is used inside the child to call the parent’s __init__.

? Code Examples

?‍?‍? Basic Inheritance Example

Here, Dog inherits from Animal, so it can use both its own methods and the parent’s methods.

? View Basic Inheritance Code
# Parent class
class Animal:
    def __init__(self, name):
        self.name = name

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

# Child class
class Dog(Animal):
    def bark(self):
        print(f"{self.name} says Woof!")

dog1 = Dog("Buddy")
dog1.speak()  # Inherited from Animal
dog1.bark()   # Defined in Dog

? Overriding Methods

The child class can override a method from the parent class to change its behavior for that specific child.

? View Overriding Code
class Cat(Animal):
    def speak(self):   # Overriding parent method
        print(f"{self.name} says Meow!")

cat1 = Cat("Whiskers")
cat1.speak()  # Whiskers says Meow!

? Using super()

Use super() to call the parent’s version of a method, then extend it with extra behavior.

? View super() Code
class Bird(Animal):
    def __init__(self, name, color):
        super().__init__(name)  # Call parent constructor
        self.color = color

    def speak(self):
        super().speak()  # Call parent method
        print(f"{self.name} chirps beautifully!")

bird1 = Bird("Parrot", "Green")
bird1.speak()

? Multiple Inheritance

A class can inherit from multiple parents. Python will follow the Method Resolution Order (MRO) to decide which parent to search first.

? View Multiple Inheritance Code
class CanFly:
    # Provides flying ability
    def fly(self):
        print("Flying high!")

class CanSwim:
    # Provides swimming ability
    def swim(self):
        print("Swimming smoothly!")

class Duck(CanFly, CanSwim):
    # Inherits both flying and swimming
    pass

duck1 = Duck()
duck1.fly()   # Uses fly() from CanFly
duck1.swim()  # Uses swim() from CanSwim

? Example Output & Explanation

  • dog1.speak()Buddy makes a sound. (from Animal).
  • dog1.bark()Buddy says Woof! (from Dog).
  • cat1.speak()Whiskers says Meow! (overridden in Cat).
  • bird1.speak() first prints the parent message, then the child’s extra message.
  • duck1.fly() and duck1.swim() show that Duck can use methods from both parents.

Notice how each child class reuses or customizes behavior from its parent, instead of duplicating code.

? Tips & Best Practices

  • Use super() to extend parent functionality without rewriting code.
  • Prefer single inheritance when possible to keep the design simple.
  • Use multiple inheritance carefully and understand the Method Resolution Order (MRO).
  • Keep base classes generic and reusable; put specific behavior into child classes.
  • Give meaningful class names that reflect the hierarchy (e.g., Animal → Dog → PetDog).

? Try It Yourself

  • Create a Vehicle class with a start() method, then extend it into Car and Bike classes.
  • Make an Employee class, then inherit Manager and Developer classes.
  • Try building a class with multiple inheritance (e.g., RobotDog that inherits from Dog and CanFly).
  • Experiment with overriding methods and calling super() to combine behaviors.