An Introduction to Inheritance in Python

Inheritance allows us to define a class that takes all the functionality from a parent class and allows us to add more. The parent class is the class being inherited from, also called a base class. The child class is the class that inherits from another class, also called derived class.

Let us see an example:

class Student:
def__init__(self,name,rollno,marks):
self.name = name
self.rollno = rollno
self.marks = marks
def Info(self):
print('Student name is ',self.name,'roll no ',self.rollno)

In the above program, the Student class is the parent class and the Result class is the subclass. We can perform the methods defined in the Student class also through the Result class.

t = Result('tom',234,75)
t.Info()

What is Composition?

Composition is a concept that models a has a relationship. It enables creating complex types by combining objects of other types. This means that a class can contain an object of another class. This relationship means that a class has a class.

Example:

class Student:
def__init__(self,name,rollno,marks):
self.name = name
self.rollno = rollno
self.marks = marks
def Info(self):
print('Student name is ',self.name,'roll no ',self.rollno)

The composition is similar to inheritance but we cannot call the methods of the class that we defined inside the result class.

Inheritance vs Composition

Inheritance is defined as ‘is a relationship’ whereas composition is defined as ‘has a relationship’. In inheritance, a class is derived as a subclass of another class whereas in composition a class is somewhat called inside another class.

Let us see an example by performing the same operations using inheritance and composition.

class Employee:
def __init__(self,name,eid):
self.name = name
self.eid = eid
def printDetails(self):
print('Employee name is {} and Employee no is {}'.format(self.name,self.eid))

Here we are printing the Employee details using two different classes.

s = Supervisior('jon',25)
t = TL('Tom',30)

We can see that we can directly call a method of a parent class in subclass using Inheritance whereas in composition we have to define it under the other module.

Types of Inheritance

Single Inheritance

Single inheritance enables a derived class to inherit properties from a single parent class, thus enabling code reusability and the addition of new features to existing code.

Example:

class P:
def mym1(self):
print('Parent Method')
class Q(P):
def mym2(self):
print('Child Method')

Multi-level Inheritance

In multilevel inheritance, features of the base class and the derived class are further inherited into the new derived class. This is similar to a relationship representing a child and a grandfather.

Example:

class P:
def mym1(self):
print('Parent Method')
class Q(P):
def mym2(self):
print('Child Method')
class R(Q):
def mym3(self):
print('Muli-level inheritance Method')
a = R()
a.mym3()
a.mym2()
a.mym1()

Multiple Inheritance

When a class can be derived from more than one base class this type of inheritance is called multiple inheritance. In multiple inheritance, all the features of the base classes are inherited into the derived class.

Example:

class P:
def mym1(self):
print('Parent1 Method')
class Q():
def mym2(self):
print('Parent2 Method')
class R(Q,R):
def mym3(self):
print('Child Method')
a = R()
a.mym3()
a.mym2()
a.mym1()

Hierarchical Inheritance

When more than one derived classes are created from a single base this type of inheritance is called hierarchical inheritance. In this program, we have a parent (base) class and two children (derived) classes.

Example:

class P:
def mym1(self):
print('Parent Method')
class Q(P):
def mym2(self):
print('Child1 Method')
class R(Q,R):
def mym3(self):
print('Child2 Method')
a = R()
b = Q()

Hybrid Inheritance

Inheritance consisting of multiple types of inheritance is called hybrid inheritance.

Cyclic Inheritance:

Two classes inheriting each other.

class P(Q): 
pass

Method Resolution Order (MRO)

The Python Method Resolution Order defines the class search path used by Python to search for the right method to use in classes having multi-inheritance.

class A:pass
class B:pass
class C:pass
class D(A):pass
class E(B):pass
class F(D,E,C):pass

That’s it. Hope you found this article useful in giving you a solid grasp of how inheritance is performed in Python. Thank you.

Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store