Python Inheritance Complete Guide
Learn Python inheritance - single, multiple, multilevel inheritance, method overriding, polymorphism, MRO, abstract classes, and OOP best practices with practical examples.
Inheritance
Parent-child relationships
Polymorphism
Many forms, one interface
Encapsulation
Data hiding
Abstraction
Hide complexity
What is Inheritance?
Inheritance is an OOP concept where one class acquires properties and methods of another class.
- Parent Class / Base Class -> Existing class
- Child Class / Derived Class -> New class that inherits features
Real-Life Example
- Father -> Parent class
- Son -> Child class
The child inherits qualities from the parent.
Why Use Inheritance?
- Code reusability
- Reduces duplication
- Easy maintenance
- Better organization
- Supports hierarchical relationships
Basic Syntax
class Parent:
pass
class Child(Parent):
pass
Simple Inheritance Example
class Animal:
def eat(self):
print("Animal eats food")
class Dog(Animal):
pass
d = Dog()
d.eat()
Output: Animal eats food
The Dog class inherited the eat() method from Animal.
Types of Inheritance in Python
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
1. Single Inheritance
One child inherits from one parent.
class Parent:
def display(self):
print("Parent class")
class Child(Parent):
pass
obj = Child()
obj.display()
2. Multiple Inheritance
One child inherits from multiple parents.
class Father:
def skills1(self):
print("Driving")
class Mother:
def skills2(self):
print("Cooking")
class Child(Father, Mother):
pass
c = Child()
c.skills1()
c.skills2()
3. Multilevel Inheritance
Inheritance chain across multiple levels.
class Grandparent:
def house(self):
print("Grandparent house")
class Parent(Grandparent):
def car(self):
print("Parent car")
class Child(Parent):
def bike(self):
print("Child bike")
c = Child()
c.house()
c.car()
c.bike()
4. Hierarchical Inheritance
Multiple child classes inherit from one parent.
class Animal:
def eat(self):
print("Eating")
class Dog(Animal):
pass
class Cat(Animal):
pass
d = Dog()
c = Cat()
d.eat()
c.eat()
5. Hybrid Inheritance
Combination of multiple inheritance types.
class A:
pass
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
Using super() Method
super() calls parent class constructor or methods.
Example of super()
class Parent:
def __init__(self):
print("Parent constructor")
class Child(Parent):
def __init__(self):
super().__init__()
print("Child constructor")
c = Child()
Output:
Parent constructor
Child constructor
Constructor Inheritance
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def show(self):
print(self.name)
s = Student("Rahul")
s.show()
Method Overriding in Inheritance
Child class can modify parent method.
class Animal:
def sound(self):
print("Animal sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
d = Dog()
d.sound()
Method Resolution Order (MRO)
MRO decides the order in which classes are searched in multiple inheritance.
class A:
pass
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
print(D.mro())
Advantages and Disadvantages of Inheritance
Advantages of Inheritance
- Reusability
- Saves time
- Easier debugging
- Better program structure
- Supports polymorphism
Disadvantages of Inheritance
- Tight coupling
- Complex hierarchy can confuse developers
- Excessive inheritance reduces readability
Real-Life Examples
| Example | Parent | Child |
|---|---|---|
| Vehicle system | Vehicle | Car, Bike |
| School | Person | Student, Teacher |
| Animal kingdom | Animal | Dog, Cat |
Key Takeaways
- Inheritance allows child classes to reuse and extend parent class functionality
- Python supports multiple inheritance using comma-separated parent classes
- Use the super() function to call parent class methods
- Method Resolution Order (MRO) determines the search order for methods in inheritance hierarchy
- Method overriding allows child classes to provide specific implementations
- Polymorphism enables objects of different classes to be treated as objects of a common superclass
- Abstract classes (using
abcmodule) define interfaces that must be implemented by subclasses - Duck typing in Python focuses on object behavior rather than type
- Operator overloading allows defining behavior for operators like
+,-,* - Use inheritance for "is-a" relationships and composition for "has-a" relationships
- Keep inheritance hierarchies shallow to avoid complexity
- The C3 linearization algorithm computes MRO in Python
- Mixins are small classes that provide specific functionality to be inherited
- Always document the purpose and relationships in your class hierarchy