
As you might know, creating a fully functional class in an object-oriented programming language is time-consuming because real classes perform a lot of complex tasks.
In Python, you can get the features you want from an existing class(parent) to create a new class(child). This Python feature is called inheritance.
By inheritance, you can
- obtain the features of a parent class,
- change the features that you don’t need,
- add new features to your child class. (derived class or subclass)
Since you are using a pre-used, tested class, you don’t have to put quite as much effort into your new class. The child class inherits the attributes and functions of its parent class.
If we have several similar classes, we can define the common functionalities of them in one class and define child classes of this parent class and implement specific functionalities there.
Three Data Professionals

We have 3 data professionals. They all know mathematics and statistics, SQL, and a programming language like Python, R, Java, etc.
class Data_Scientist:
def __init__(self, name, SQL):
self.name = name
self.SQL = SQL
def knows_maths_stats(self):
return True
def knows_programming(self):
return True
class Data_Analyst:
def __init__(self, name, SQL):
self.name = name
self.SQL = SQL
def knows_maths_stats(self):
return True
def knows_programming(self):
return True
class Data_Engineer:
def __init__(self, name):
self.name = name
self.SQL = SQL
def knows_maths_stats(self):
return True
def knows_programming(self):
return True
Instead of writing the same class again and again, we can define a parent class "Data_Professional" and 3 child classes of the Data_Professional class: Data_Analyst, Data_Scientist, and Data_Engineer.
Parent Class
The class from which we inherit called the Parent Class, Superclass, or Base Class.
class Data_Professional:
def __init__(self, name, SQL):
self.name = name
self.SQL = SQL
def knows_maths_stats(self):
return True
def knows_programming(self):
return True
Child Classes
The new class which was created by inheriting functionalities of the parent class is called Child Class, Derived Class, or Subclass. The child class’ init() function overrides the parent class’s init() function.
Accessing Parent Class Variables From Child Class
Mary Smith is a data scientist who knows maths and stats, SQL, and Python.
1. Define the child class
class Data_Scientist(Data_Professional):
def __init__(self, name, SQL):
super().__init__(name, SQL)
2. Initialize dt_sci object:
dt_sci = Data_Scientist("Mary Smith", 9)
3. Print the features of dt_sci:
print(dt_sci.name,
dt_sci.SQL,
dt_sci.knows_maths_stats(),
dt_sci.knows_programming())
Mary Smith 9 True True
An empty class has the properties of its parent class
class Data_Engineer(Data_Professional):
pass
dt_en = Data_Engineer("Hyun-mi Tokko", 9)
print(dt_en.name,
dt_en.SQL,
dt_en.knows_maths_stats(),
dt_en.knows_programming())
Hyun-mi Tokko 9 True True
Overriding Classes
class Data_Analyst(Data_Professional):
def __init__(self, name, SQL):
super().__init__(name, SQL)
def knows_programming(self):
return False
dt_an = Data_Analyst("Brinda Kumar", 7)
print(dt_an.name,
dt_an.SQL,
dt_an.knows_maths_stats(),
dt_an.knows_programming())
Brinda Kumar 7 True False
Checking Subclasses And Instances
issubclass() Method:
The Builtin function issubclass(class1, class2) asks whether one class is a subclass of another class.
print(issubclass(Data_Analyst, Data_Professional))
True
print(issubclass(Data_Professional, Data_Analyst))
False
isinstance() Method
The builtin function isinstance(object, class) asks whether an object is an instance of a class.
print(isinstance(dt_an, Data_Analyst))
True
print(isinstance(dt_en, Data_Analyst))
False
Types Of Inheritance
There are several types of inheritance. These are single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance, and hybrid inheritance. The first four of them are illustrated below. Hybrid inheritance is a mixture of these four types of inheritances. The example above is an example of hybrid inheritance.

Advantages Of Inheritance
Inheritance provides a lot of advantages. By using parent and child classes,
- we can write more reusable, flexible code which is easy to extend by new developers,
- we can write more organized, structured, readable code which helps us debug and identify errors easily,
- we can reduce the code repetition and duplication, we can put all the common attributes and functions into the parent class and access them by the child class.
Conclusion
Inheritance is one of the most critical concepts in Object Oriented Programming (OOP). It allows us to create new classes based on an existing class or multiple classes. We will discuss polymorphism, encapsulation, and abstraction in the next blogs.
Contact me
If you want to be updated with my latest articles follow me on Medium. You can connect with me on LinkedIn, and email me at [email protected]!
Any suggestions and comments will be very appreciated!