Table of Contents
- Introduction to Inheritance
- How Inheritance Works in Python
- Types of Inheritance
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Method Resolution Order (MRO)
- Practical Examples
- Conclusion
Introduction to Inheritance
Inheritance is a cornerstone of object-oriented programming (OOP) in Python. It allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class). This promotes code reusability and establishes a natural hierarchy between classes.
How Inheritance Works in Python
In Python, inheritance is implemented by defining a child class that specifies its parent class in parentheses. The child class automatically gains access to the parent’s attributes and methods but can also override or extend them.
For example:
class Parent:
def greet(self):
return "Hello from Parent!"
class Child(Parent):
pass
child = Child()
print(child.greet()) # Output: Hello from Parent!
The Child
class inherits the greet
method from the Parent
class without defining it explicitly.
Types of Inheritance
Python supports several types of inheritance, each suited for different use cases.
Single Inheritance
Single inheritance involves one child class inheriting from one parent class.
class Animal:
def speak(self):
return "I make a sound"
class Dog(Animal):
def speak(self):
return "Woof!"
dog = Dog()
print(dog.speak()) # Output: Woof!
Here, Dog
overrides the speak
method of Animal
.
Multiple Inheritance
Multiple inheritance allows a child class to inherit from more than one parent class.
class Flyer:
def fly(self):
return "I can fly!"
class Swimmer:
def swim(self):
return "I can swim!"
class Duck(Flyer, Swimmer):
pass
duck = Duck()
print(duck.fly()) # Output: I can fly!
print(duck.swim()) # Output: I can swim!
Duck
inherits methods from both Flyer
and Swimmer
.
Multilevel Inheritance
Multilevel inheritance involves a chain of inheritance, where a class inherits from a parent, which itself inherits from another parent.
class Vehicle:
def move(self):
return "I move"
class Car(Vehicle):
def honk(self):
return "Beep!"
class ElectricCar(Car):
def charge(self):
return "Charging..."
tesla = ElectricCar()
print(tesla.move()) # Output: I move
print(tesla.honk()) # Output: Beep!
print(tesla.charge()) # Output: Charging...
ElectricCar
inherits from Car
, which inherits from Vehicle
.
Hierarchical Inheritance
Hierarchical inheritance involves multiple child classes inheriting from the same parent class.
class Shape:
def draw(self):
return "Drawing a shape"
class Circle(Shape):
def draw(self):
return "Drawing a circle"
class Square(Shape):
def draw(self):
return "Drawing a square"
circle = Circle()
square = Square()
print(circle.draw()) # Output: Drawing a circle
print(square.draw()) # Output: Drawing a square
Both Circle
and Square
inherit from Shape
.
Method Resolution Order (MRO)
In multiple inheritance, Python uses the Method Resolution Order (MRO) to determine the order in which parent classes are searched for attributes or methods. The MRO follows the C3 linearization algorithm and can be inspected using the __mro__
attribute or mro()
method.
class A:
def greet(self):
return "Hello from A"
class B(A):
pass
class C(A):
def greet(self):
return "Hello from C"
class D(B, C):
pass
print(D.__mro__)
# Output: (<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>)
d = D()
print(d.greet()) # Output: Hello from C
Here, D
inherits from B
and C
, but C
’s greet
method is used because C
appears before A
in the MRO.
Practical Examples
Let’s consider a real-world example of a school management system:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
return f"Hi, I'm {self.name}, {self.age} years old."
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
def introduce(self):
return f"{super().introduce()} I'm a student with ID {self.student_id}."
class Teacher(Person):
def __init__(self, name, age, subject):
super().__init__(name, age)
self.subject = subject
def introduce(self):
return f"{super().introduce()} I teach {self.subject}."
student = Student("Alice", 20, "S123")
teacher = Teacher("Bob", 35, "Math")
print(student.introduce()) # Output: Hi, I'm Alice, 20 years old. I'm a student with ID S123.
print(teacher.introduce()) # Output: Hi, I'm Bob, 35 years old. I teach Math.
This example demonstrates hierarchical inheritance and the use of super()
to call parent class methods.
Conclusion
Inheritance in Python is a powerful mechanism for building modular and reusable code. By understanding single, multiple, multilevel, and hierarchical inheritance, you can design flexible class hierarchies tailored to your application’s needs. The Method Resolution Order ensures predictable behavior in complex inheritance scenarios. Whether you’re building simple scripts or large-scale systems, mastering inheritance will enhance your ability to write clean, efficient, and maintainable Python code.