Inheritance in Python

Introduction
Inheritance is one of the four fundamental pillars of Object-Oriented Programming (OOPs). It is an important concept that allows us to create new classes using existing classes.
In Python, inheritance allows a child class to acquire the attributes and methods of a parent class. Instead of writing the same code repeatedly, we can reuse existing code and extend its functionality whenever required.
Inheritance helps make code:
- Reusable
- Organized
- Maintainable
- Easier to extend
Let's understand how inheritance works in Python.
1. What is Inheritance?
Inheritance is an OOP concept in which one class inherits the properties and behaviours of another class.
There are two important terms to understand:
- Parent Class (Base Class): The class whose attributes and methods are inherited.
- Child Class (Derived Class): The class that inherits attributes and methods from the parent class.
For example, consider a general class called Animal. It has common behaviours such as eating and sleeping.
Now, we can create child classes such as Dog and Cat that inherit these common behaviours from the Animal class.
Instead of defining eating and sleeping separately in every class, we can define them once in the parent class and reuse them.
2. Syntax of Inheritance
The syntax for inheritance in Python is:
class Parent:
# Parent class attributes and methods
pass
class Child(Parent):
# Child class attributes and methods
pass
Here, the child class inherits the properties and methods of the parent class by specifying the parent class name inside parentheses.
3. Understanding Inheritance with an Example
Let's understand inheritance with a simple example.
class Animal:
def eat(self):
print("Animal is eating")
def sleep(self):
print("Animal is sleeping")
class Dog(Animal):
def bark(self):
print("Dog is barking")
dog = Dog()
dog.eat()
dog.sleep()
dog.bark()
Output:
Animal is eating
Animal is sleeping
Dog is barking
Explanation:
- We created a parent class called
Animal. - The
Animalclass contains two methods:eat()andsleep(). - We created a child class called
Dogthat inherits fromAnimal. - The
Dogclass has its own method calledbark(). - When we create an object of the
Dogclass, it can access both the inherited methods and its own method.
This demonstrates how inheritance promotes code reusability.
4. Types of Inheritance in Python
Python supports five major types of inheritance:
1. Single Inheritance
Single inheritance occurs when a child class inherits from only one parent class.
Example:
class Parent:
def display(self):
print("This is the parent class")
class Child(Parent):
def show(self):
print("This is the child class")
obj = Child()
obj.display()
obj.show()
Output:
This is the parent class
This is the child class
Here, the Child class inherits the display() method from the Parent class.
2. Multiple Inheritance
Multiple inheritance occurs when a child class inherits from more than one parent class.
Example:
class Mother:
def skills(self):
print("Mother: Programming")
class Father:
def talent(self):
print("Father: Painting")
class Child(Mother, Father):
pass
obj = Child()
obj.skills()
obj.talent()
Output:
Mother: Programming
Father: Painting
Here, the Child class inherits methods from both Father and Mother.
3. Multilevel Inheritance
Multilevel inheritance occurs when a class inherits from another child class, creating a chain of inheritance.
Example:
class Grandfather:
def house(self):
print("Grandfather owns a house")
class Father(Grandfather):
def car(self):
print("Father owns a car")
class Child(Father):
def bike(self):
print("Child owns a bike")
obj = Child()
obj.house()
obj.car()
obj.bike()
Output:
Grandfather owns a house
Father owns a car
Child owns a bike
Here, the Child class inherits from Father, which itself inherits from Grandfather.
Therefore, the child class can access methods throughout the inheritance chain.
4. Hierarchical Inheritance
Hierarchical inheritance occurs when multiple child classes inherit from the same parent class.
Example:
class Animal:
def eat(self):
print("Animal is eating")
class Dog(Animal):
def bark(self):
print("Dog is barking")
class Cat(Animal):
def meow(self):
print("Cat is meowing")
dog = Dog()
cat = Cat()
dog.eat()
dog.bark()
cat.eat()
cat.meow()
Output:
Animal is eating
Dog is barking
Animal is eating
Cat is meowing
Here, both Dog and Cat inherit the eat() method from the same parent class, Animal.
5. Hybrid Inheritance
Hybrid inheritance is a combination of two or more types of inheritance.
Example:
class A:
def display(self):
print("Class A")
class B(A):
pass
class C(A):
pass
class D(B, C):
pass
obj = D()
obj.display()
Output:
Class A
Here, we combine hierarchical and multiple inheritance.
Both B and C inherit from A, while D inherits from both B and C.
Python uses Method Resolution Order (MRO) to determine the order in which methods are searched.
7. Advantages of Inheritance
Inheritance provides several advantages in Python:
- Code Reusability: We can reuse existing methods and attributes without writing them again.
- Code Maintainability: Changes to shared functionality can be made in the parent class.
- Extensibility: We can create new classes and add functionality without modifying the existing parent class.
- Better Organization: It helps organize related classes into a logical hierarchy.
- Supports Polymorphism: Method overriding allows different child classes to provide different implementations of the same method.
Conclusion
Inheritance is an important concept in Object-Oriented Programming that allows us to reuse and extend existing code.
In the next article, we will explore Polymorphism in Python, another fundamental pillar of Object-Oriented Programming.
About the author
- I'm a software engineer, entrepreneur, and lifelong learner. Beyond coding, I'm interested in sports and business. This blog is where I share my journey, technical insights, and experiences from building products and startups.
Comments
Join the discussionNo comments yet. Be the first to comment.




