🧱 Python Object-Oriented Programming (OOP)
Estimated reading: 3 minutes 273 views

Python Inheritance – Reuse and Extend Class Functionality

Introduction – Why Use Inheritance in Python?

In object-oriented programming, inheritance allows one class to reuse attributes and methods of another. It improves code reusability, encourages DRY (Don’t Repeat Yourself) principles, and allows for logical class hierarchy design.

Python supports multiple types of inheritance, including single, multiple, multilevel, and hierarchical.

In this guide, you’ll learn:

  • What inheritance is and why it matters
  • The different types of inheritance in Python
  • How to use super() to access parent methods
  • Real-world examples
  • Best practices and common mistakes

What Is Inheritance?

Inheritance is a mechanism where a new class (child or subclass) inherits properties and behaviors from an existing class (parent or base class).

class Parent:
    def speak(self):
        print("Speaking from Parent")

class Child(Parent):
    pass

c = Child()
c.speak()  # Inherits speak() from Parent

Output:

Speaking from Parent

Types of Inheritance in Python

TypeDescription
SingleOne child class inherits from one parent class
MultipleOne child class inherits from multiple parents
MultilevelClass inherits from a class that is also a child
HierarchicalMultiple children inherit from one parent

1. Single Inheritance

class Animal:
    def sound(self):
        return "Some sound"

class Dog(Animal):
    def breed(self):
        return "Labrador"

d = Dog()
print(d.sound())  # Inherited
print(d.breed())  # Own method

2. Multiple Inheritance

class Father:
    def skills(self):
        return ["Gardening", "Carpentry"]

class Mother:
    def skills(self):
        return ["Cooking", "Art"]

class Child(Father, Mother):
    def skills(self):
        return super().skills() + ["Python"]

c = Child()
print(c.skills())  # Output depends on MRO (Method Resolution Order)

3. Multilevel Inheritance

class Grandparent:
    def speak(self):
        print("Hello from grandparent")

class Parent(Grandparent):
    def listen(self):
        print("Listening in parent")

class Child(Parent):
    def play(self):
        print("Playing in child")

c = Child()
c.speak()
c.listen()
c.play()

4. Hierarchical Inheritance

class Vehicle:
    def start(self):
        print("Engine started")

class Bike(Vehicle):
    pass

class Car(Vehicle):
    pass

Using super() Function

Use super() to call methods of the parent class:

class Person:
    def greet(self):
        print("Hello!")

class Student(Person):
    def greet(self):
        super().greet()
        print("Hi, I’m a student.")

Output:

Hello!
Hi, I’m a student.

Common Mistakes in Inheritance

Mistake Fix
Overriding without super()Always use super() to extend parent functionality
Ignoring MRO in multiple inheritanceUse consistent method resolution order
Mixing unrelated classes in hierarchyUse composition instead of inheritance when needed

Best Practices for Python Inheritance

Do This Avoid This
Use inheritance to express “is-a” relationshipsForcing unrelated classes into inheritance
Favor composition over multiple inheritanceDeep, confusing inheritance chains
Use super() for clean extensibilityDuplicating code from the parent class
Keep your base class focused and reusableMaking the base class too general or large

Summary – Recap & Next Steps

Inheritance in Python allows developers to reuse and extend class behavior, enabling code reuse and logical class structure.

Key Takeaways:

  • Inheritance enables a child class to reuse parent attributes/methods
  • Use super() to call parent class methods inside overrides
  • Types: Single, Multiple, Multilevel, Hierarchical
  • Avoid deep inheritance chains and misuse of super()

Real-World Relevance:
Used in frameworks (like Django models), custom libraries, and domain modeling for building scalable software.


FAQ – Python Inheritance

What is inheritance in Python?

Inheritance is a mechanism where a new class derives behavior and properties from an existing class.

Can a class inherit from multiple classes?

Yes. Python supports multiple inheritance using comma-separated base classes.

class C(A, B): ...

What does super() do?

super() allows you to call methods of a parent class inside a child class, commonly used when overriding methods.

How do I know the method resolution order (MRO)?

Use:

print(ClassName.__mro__)

Or:

help(ClassName)

Is inheritance better than composition?

Use inheritance for “is-a” relationships, and composition for “has-a” relationships. Composition is often more flexible.


Share Now :
Share

Python Inheritance

Or Copy Link

CONTENTS
Scroll to Top