𧬠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
Type | Description |
---|---|
Single | One child class inherits from one parent class |
Multiple | One child class inherits from multiple parents |
Multilevel | Class inherits from a class that is also a child |
Hierarchical | Multiple 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 inheritance | Use consistent method resolution order |
Mixing unrelated classes in hierarchy | Use composition instead of inheritance when needed |
π Best Practices for Python Inheritance
β Do This | β Avoid This |
---|---|
Use inheritance to express βis-aβ relationships | Forcing unrelated classes into inheritance |
Favor composition over multiple inheritance | Deep, confusing inheritance chains |
Use super() for clean extensibility | Duplicating code from the parent class |
Keep your base class focused and reusable | Making 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 :