🧱 Python Object-Oriented Programming (OOP)
Estimated reading: 3 minutes 28 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Python Inheritance

Or Copy Link

CONTENTS
Scroll to Top