Types of Inheritance in Python
Inheritance in Python is a mechanism that allows a new class to inherit all the attributes and methods of an existing class. It is one of the most important feature of OOPs concept in Python because it promotes reusability, readability, and efficiency.
This feature is developed after the C programming language. In programming, we often need to use the same code blocks in many places. Instead of writing the same code blocks in many places, put the same code blocks in one place and use them as per needed anywhere without defining again.
To reduce this code redundancy, we use inheritance feature. Hence, the main purpose of implementing inheritance in Python or any other programming languages, such as Java, is to reuse the code.
The class from which a class inherits all the attributes and methods is called parent class, superclass, or base class. A class which inherits all attributes and methods from a parent class is called subclass, derived class, or child class.
To define a class that will inherit all the methods and variables (i.e. attributes) from a parent class, we will use the below syntax:
class ChildClass(ParentClass):
This syntax is very simple than that is used in Java or C++ for inheriting classes.The ChildClass represents the new class, and the ParentClass represents the existing or old class followed by a colon ( : ). The body of the class starts with an indentation.
This is the basic introduction of inheritance concept. In this tutorial, we will understand different types of inheritance in Python programming with the help of examples.
Types of Inheritance in Python with Examples
Like C++ or Java technology, Python also supports different types of inheritance, each with its own unique characteristics. Depending upon the number of child and parent classes involved, Python supports five types of inheritance that are as follows:
- Single inheritance
- Multilevel inheritance
- Multiple inheritance
- Hierarchical inheritance
- Hybrid inheritance
The arrow in the figure expresses the concept of derivation or inheritance. The direction of the arrow, which goes from the derived class towards the base class, represents that the derived class inherits all the features (attributes and methods) of the base class, but not vice versa.
Inheritance in object-oriented programming, including Python, follows this direction, where child or derived classes inherit properties from parent or base classes. This terminology is commonly used to describe the relationship between classes in object-oriented programming, where the derived class inherits attributes and methods from the parent class.
Let’s understand them one by one with the help of examples.
Single Inheritance:
When a child class inherits the properties (i.e. variables) and behavior (i.e. methods) from only a single parent class, it is called single inheritance in Python. In the figure, class B inherits from class A. So, class B is derived class or child class and class A is parent class or base class. This relationship represents single inheritance.
The general syntax for a single inheritance is as:
class ParentClass:
# ParentClass attributes and methods
class ChildClass(ParentClass):
# ChildClass attributes and methods
Let’s take a simple example based on the single inheritance.
Example 1:
# Python program to demonstrate single inheritance.
# Creating a base class named Animal.
class Animal:
# Defining a function inside the base class.
def speak(self):
print("Animal speaks")
# Creating a derived class named Dog.
class Dog(Animal):
# Defining a function inside the derived class.
def bark(self):
print("Dog barks")
# Outside the class definition.
# Creating an instance of Dog
dog = Dog()
# Calling functions of both base and derived classes.
dog.speak()
dog.bark()
# Creating an instance of Animal class.
an = Animal()
# Calling the function of base class.
an.speak()
Output: Animal speaks Dog barks Animal speaks
In this example, the child class Dog inherits the methods from its parent class Animal. The child class Dog has also has its own method defined. Note that “an” is an instance of Animal class and can only access speak() method because the reference variable an refers to the object of Animal class.
Whereas, “dog” is an instance of Dog class and can access both methods from Animal class and its own class because the reference variable dog is pointing to the object of Dog class.
With inheritance, the method from the parent class (i.e. Animal class) automatically comes in the child class (i.e. Dog class) by default. In other words, by default, the method from the parent class is copied into the child class. Therefore, we can access both methods from the parent class as well as child class using object reference variable dog.
For more practice, go to this tutorial: Single inheritance in Python for the best practice
Multilevel Inheritance:
When a class is derived from a class which is also derived from another class, it is called multilevel inheritance. In the figure, class C inherits from class B which inherits from class A. So, class C is child class of class B (parent class) which is the child class of class A (grand parent class). This relationship represents multilevel inheritance.
The general syntax for multilevel inheritance is as follows:
class GrandParentClass:
# Grandparent attributes and methods
class ParentClass(GrandParentClass):
# Parent attributes and methods
class ChildClass(ParentClass):
# Child attributes and methods
In the above syntax, GrandParentClass is the name of the grandparent class, which contains attributes and methods that we want to inherit in the parent class.
ParentClass is the name of the parent class, which inherits attributes and methods from the Grandparent class. It may also contain attributes and methods specific to the parent class. ChildClass is the name of the child class, which inherits from the Parent class. It may also contain attributes and methods specific to the child class.
With multilevel inheritance, each class in the hierarchy inherits attributes and methods from its parent class, allowing us to build upon and extend functionality while maintaining a hierarchical relationship between classes. Python allows achieving multilevel inheritance to an unlimited number of levels.
Let’s take an example program based on the multilevel inheritance in Python.
Example 2:
# Python program to demonstrate multilevel inheritance.
# This class is a superclass of Parent class.
class Grandparent:
def greet(self):
print("Hello from Grandparent")
# This class is child class of Grandparent class but parent class of Child class.
class Parent(Grandparent):
def talk(self):
print("Parent talks")
# This class is child class of Parent class.
class Child(Parent):
def play(self):
print("Child plays")
# Creating an instance of Grandparent class.
grandparent = Grandparent()
# Accessing method of Grandparent class.
grandparent.greet()
# Creating an instance of Parent class.
parent = Parent()
# Accessing methods of Parent class.
parent.greet()
parent.talk()
# Creating an instance of Child
child = Child()
# Accessing methods of child class.
child.greet()
child.talk()
child.play()
Output: Hello from Grandparent Hello from Grandparent Parent talks Hello from Grandparent Parent talks Child plays
In this example, the Child class inherits methods from the Parent class, which, in turn, inherits method from the Grandparent class.
For more practice, go to this tutorial: Advanced multilevel inheritance example programs
Multiple Inheritance:
Python supports multiple inheritance. If we compare to Java, it does not support directly through classes. Java supports multiple inheritance through interface, not via classes.
When a class inherits attributes and methods from more than one parent class, this type of inheritance is called multiple inheritance in Python. It refers to two or more parent classes and one child class. In the figure, class C inherits from class A and class B. So, class C is child class of both class A and class B. This relationship represents multiple inheritance.
The general syntax for multiple inheritance is as follows:
class ParentClass1:
# ParentClass1 attributes and methods
class ParentClass2:
# ParentClass2 attributes and methods
class ChildClass(ParentClass1, ParentClass2):
# ChildClass attributes and methods
In the above syntax, ParentClass1 and ParentClass2 are the names of two parent classes from which we want to inherit attributes and methods. We can specify multiple parent classes by separating them with commas.
Whereas, ChildClass is the name of the child class that inherits attributes and methods from both ParentClass1 and ParentClass2. It contains attributes and methods specific to the child class.
With multiple inheritance, the ChildClass can inherit and use attributes and methods from both parent classes. This allows us to combine functionality from different sources and build more complex classes.
Let’s take an example program based on the concept of multiple inheritance in Python.
Example 3:
# Python program to demonstrate multiple inheritance.
# Create the first parent class.
class A:
def method_A(self):
print("Method A from class A")
# Create the second parent class.
class B:
def method_B(self):
print("Method B from class B")
# Create a child class that inherits methods from both parent classes A and B.
class C(A, B):
def method_C(self):
print("Method C from class C")
# Creating an instance of C
c = C()
# Calling methods of class C.
c.method_A()
c.method_B()
c.method_C()
Output: Method A from class A Method B from class B Method C from class C
In this example, the class C inherits methods from both classes A and B.
For more practice, go to this tutorial: Advanced multiple inheritance example programs
Hierarchical Inheritance:
When more than one child class is derived from a single parent class, this type of inheritance is called hierarchical inheritance in Python. It involves multiple classes inheriting from a single parent class or the same class. In the above figure, multiple classes, such as class B, class C, and class D is derived from a single parent class A.
So, class B, class C, and class D are child classes and class A is a parent class. This relationship represents hierarchical inheritance. The general syntax for hierarchical inheritance is as follows:
class ParentClass:
# ParentClass attributes and methods
class ChildClass1(ParentClass):
# ChildClass1 attributes and methods
class ChildClass2(ParentClass):
# ChildClass2 attributes and methods
# Additional child classes can also inherit from ParentClass
In the above syntax, ParentClass is the name of the parent class, containing attributes and methods that we want to inherit in the child classes.
ChildClass1 and ChildClass2 are the names of the child classes that inherit attributes and methods from the ParentClass. Each child class can have its own attributes and methods in addition to those inherited from the parent class.
With hierarchical inheritance, multiple child classes can inherit the properties and methods from the same parent class. This allows us to reuse code and build a family of classes that share common properties while having their unique properties.
Let’s take an example program based on the hierarchical inheritance in Python.
Example 4:
# Python program to demonstrate hierarchical inheritance.
# Create a parent class named Vehicle.
class Vehicle:
def start_engine(self):
print("Engine started")
# This class is a subclass of superclass Vehicle and inherits the method from the superclass.
class Car(Vehicle):
def drive(self):
print("Car is driving")
# This class is also a subclass of superclass Vehicle and inherits the method from the superclass.
class Bike(Vehicle):
def ride(self):
print("Bike is riding")
# Creating instances of Car and Bike
car = Car()
bike = Bike()
# Calling methods of child classes using object reference variable car and bike.
car.start_engine()
bike.start_engine()
car.drive()
bike.ride()
Output: Engine started Engine started Car is driving Bike is riding
In the above example, we can see that class Car and Bike is being inherited from a single parent class Vehicle. Therefore, both child classes, Car and Bike can access members of its parent class Vehicle as well as their own class members. But child classes Car and Bike cannot access the members of each other.
Hybrid Inheritance:
Hybrid inheritance in Python is a combination of hierarchical inheritance and multiple inheritance within a single program or class hierarchy. It allows us to create complex class structures that can serve various purposes.
From the above figure, it is clear that above the half portion represents hierarchical inheritance and below the half portion represents multiple inheritance. This relationship represents hybrid inheritance.
The general syntax for hybrid inheritance is as follows:
class ParentClass1:
# ParentClass1 attributes and methods.
class ChildClass1(ParentClass1):
# ChildClass1 attributes and methods.
class ChildClass2(ParentClass1):
# ChildClass2 attributes and methods.
class ChildClass3(ParentClass1, ParentClass2):
# ChildClass3 attributes and methods.
In this simple syntax, ParentClass1 is the superclass of both child classes ChildClass1 and ChildClass2. This shows the hierarchical inheritance. ChildClass3 demonstrates multiple inheritance, inheriting from both ParentClass1 and ParentClass2. This is the basic syntax showing the concept of hybrid inheritance.
Let’s take an example program based on the concept of hybrid inheritance.
Example 5:
# Python program to demonstrate hybrid inheritance.
class A:
def m1(self):
print("m1 in class A")
class B(A):
def m2(self):
print("m2 in class B")
class C(A):
def m3(self):
print("m3 in class C")
class D(B, C):
def m4(self):
print("m4 in class D")
# Create an instance of class D.
d = D()
# Calling the methods of class D.
d.m1()
d.m2()
d.m3()
d.m4()
Output: m1 in class A m2 in class B m3 in class C m4 in class D
In this tutorial, we have explained different types of inheritance commonly used in Python with the help of important examples. Hope that you will have understood the basic concept of each type of inheritance and practiced all programs.
Thanks for reading!!!