Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Home About Us Privacy Policy Contact Us   

HOME ABOUT US PRIVACY POLICY CONTACT US TERMS AND CONDITIONS 

Types of Inheritance in Python SOCIAL COUNTER

 ANKIT PRATAP SINGH  OCTOBER 02, 2022  0COMMENTS


 Facebook
 SHARE:  Facebook  Twitter  Pinterest

 Twitter

Types of Inheritance in Python


 Follow up on Linkedin
In this tutorial you are going to learn about types of Inheritance in Python. Here we will discuss all different types of
inheritance in python like single inheritance, Multiple inheritance, Multilevel Inheritance and Hierarchical inheritance Follow us on Instagram

Prerequisites:
FACEBOOK

Before starting this we assume that you are aware of the following Python Programming topics:

 Basic Python Programing 


RECENT POSTS
 Inheritance in Python

Types of Inheritance in Python

 OCTOBER 02, 2022

What is inheritance in python


 SEPTEMBER 27, 2022

How to create pascal triangle in python


 SEPTEMBER 24, 2022

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.

Advantages of inheritance in Python:

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.

3. This gives us a great representation of relationship in the real world. 

Single Inheritance in Python

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:

This is parent class function.


This is child class function.

Multiple Inheritance in Python.

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

class flyingCar(Airopalne, Car):


    def parents(self):
        print("Airplane :", self.airplanename)
        print("Car :", self.carname)

# Driver's code
s1 = flyingCar()
s1.airplanename = "Airbus"
s1.carname = "Lamborghini"
s1.parents()

Output:

Airplane : Airbus
Car : Lamborghini

Multilevel inheritance in Python

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

# Parent class (Grand Father)

class GrandfatherCar:

    def __init__(self, gfcarname):


        self.gfcarname = gfcarname

# Child class (Father)

class FatherCar(GrandfatherCar):
    def __init__(self, fcarname, gfcarname):
        self.fcarname = fcarname

        # using constructor of Grandfather class


        GrandfatherCar.__init__(self, gfcarname)

# New Child class (Grand Son) 

class SonCar(FatherCar):
    def __init__(self, scarname, fcarname, gfcarname):
        self.scarname = scarname

        # using constructor of Father class


        FatherCar.__init__(self, fcarname, gfcarname)

    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:

Grandfather Car name : Ford Roadster


Father car name : Ford GTX
Son car name : Ford Muroc

Hierarchical inheritance in Python

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:

This is parent class function.


This is child 1 class function.
This is parent class function.
This is child 2 class function.

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.

LABELS: INHERITANCE IN PYTHON PYTHON TYPES OF INHERITANCE IN PYTHON

 SHARE:  Facebook  Twitter  Pinterest


YOU MIGHT ALSO LIKE

Types of Inheritance in Python


 OCTOBER 02, 2022

POST A COMMENT

No comments

To leave a comment, click the button below to sign in with Google.

SIGN IN WITH GOOGLE

ABOUT AROHANA LABLES BLOG ARCHIVE

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)

© Arohana - Rising Upwards 2022 .| Created By SoraTemplates   

You might also like