Professional Documents
Culture Documents
Types of Inheritance in Python
Types of Inheritance in Python
Prerequisites:
FACEBOOK
Before starting this we assume that you are aware of the following Python Programming topics:
Inheritance in Python
Object-oriented programming has a major role in the development of high-level programming language. Object
Oriented Programming gives us many advantages such as code reusability and flexibility. Inheritance is an important
concept of Object Oriented Programming.
Inheritance in Python is a significant separate importance of its own. Inheritance is a type of mechanism that allows
one class to inherit all the features and properties of another class.
There are mainly two classes in inheritance, one is the parent class or base class and the other is the child class or
derived class. Parent class is the class that is inherited and the child class is the class that inherits the parent class.
1. It provides us with code reusability which means that we do not need to write the same code over and over again.
It allows us to add new functionality to the existing one without changing it.
2. It is of a transitive nature. This means that if class A is inherited by class B, then all the subclasses belonging to
class A will also inherit directly from class B.
Single inheritance is one of the types of inheritance in Python, Single inheritance let the child class or derived class to
inherit properties from a single parent class or base class. It enable the code reusability and let add new feature to
existing code.
# single inheritance
# Parent class
class Car:
def function1(self):
print("This is parent class function.")
# Child class
class bmw(Car):
def function2(self):
print("This is child class function.")
# Driver's code
obj = bmw()
obj.function1()
obj.function2()
Output:
Multiple inheritance is one of the types of inheritance in Python. Multiple inheritance is an inheritance in which a child
class or derived class inherits more than one parent class or base class. In multiple inheritance all the features of the
parent class are inherited by the child class.
# multiple inheritance
# Parent class1
class Airopalne:
airplanename = ""
def airplane(self):
print(self.airplanename)
# Parent class2
class Car:
carname = ""
def car(self):
print(self.carname)
# Child class
# Driver's code
s1 = flyingCar()
s1.airplanename = "Airbus"
s1.carname = "Lamborghini"
s1.parents()
Output:
Airplane : Airbus
Car : Lamborghini
Multilevel inheritance is also one of the types of inheritance in Python. Multilevel inheritance in python is similar to
grandfather and grandson, as a grandson will be inheriting two people simultaneously, one is his grandfather and the
other his father. Similarly multilevel inheritance also works in which all the features of parent class and child class will
be inherited by a new child class.
# multilevel inheritance
class GrandfatherCar:
class FatherCar(GrandfatherCar):
def __init__(self, fcarname, gfcarname):
self.fcarname = fcarname
class SonCar(FatherCar):
def __init__(self, scarname, fcarname, gfcarname):
self.scarname = scarname
def carname(self):
print('Grandfather Car:', self.gfcarname)
print("Father car :", self.fcarname)
print("Son car :", self.scarname)
# Driver code
s1 = SonCar('Ford Muroc','Ford GTX','Ford Roadster')
print(s1.gfcarname)
s1.carname()
Output:
Hierarchical inheritance in Python is also one of the types of Inheritance in Python. Hierarchical inheritance is an
inheritance in which there is a parent class and it has more than one child class i.e. in this one parent class is
inherited by more than one child child class. Like two children of one parent.
# Hierarchical inheritance
# Parent class
class Car:
def function1(self):
print("This is parent class function.")
# child class1
class Audi(Car):
def function2(self):
print("This is child 1 class function.")
# Child class2
class Lamborghini(Car):
def function3(self):
print("This is child 2 class function.")
# Driver's code
obj1 = Audi()
obj2 = Lamborghini()
obj1.function1()
obj1.function2()
obj2.function1()
obj2.function3()
Output:
Conclusion:
So this the end of this tutorial. Here we have leant about different types of inheritance in Python. We learned about
the concept of inheritance and also about its benefits and after that we learned about Single Inheritance, Multiple
Inheritance, Multilevel Inheritance and Hierarchical Inheritance in Python.
POST A COMMENT
No comments
Aarohan is a blog whose purpose is to keep CODING DJANGO PROGRAM PYTHON ▼ 2022(30)
Python/Django C/C++ in front of the world in a very ▼ October(1)
WHAT TO DO AFTER 12TH
simple way so that those who do not have the Types of Inheritance in Python
resources but have a strong desire to learn, they ► September(7)
can easily learn these subjects.
► August(6)
Learn More → ► July(6)
► June(4)
► May(4)
► March(1)
► February(1)