top of page

Python Inheritance

What is Python Inheritance?

In python, inheritance is the process of extracting or find the value of parent class or base class by child class and use it by the child class.

It is an oops concepts.

Syntax

>>> Class class1:

.

.

>>> Class class2(class1)

.

.

>>> obj = class2()

Now we have to go through a basic example so we can learn it easily.

>>> class first_name():

            def first(self):

                print('naveen')

 

     class last_name(Parent):

            def last(self):

               print('kumar')

 

     obj = last_name()

     obj.first()

     obj.last()

Now in the next tutorial, we will learn about "Python Dates", python inheritance is important to start with python oops so please read it carefully and try it with some useful examples.

Types of Python Inheritance?

Python uses the different types of inheritances which is given as below:

  • Single Inheritance

  • Multiple Inheritance

  • Multilevel Inheritance

  • Hierarchical Inheritance

  • Hybrid Inheritance

Single Inheritance:

 

When child class inherit from only one base class called the single inheritance

Example:

class Base:

     def f1(self):

          print("this is first base class")

class Sub(Base):

     def f2(self):

          print(" this is child class ")

obj = Sub()

obj.f1()

obj.f2()

Multiple Inheritance:

 

When child class inherit from more than one base class called the multiple inheritance

Example:

class Base:

     def f1(self):

          print("this is first bas class")

class Base2:

     def f2(self):

          print("This is second base class")

class Sub(Base, Base2):

     def f3(self):

          print(" this is child funciton ")

obj = Sub()

obj.f1()

obj.f2()

obj.f2()

Multilevel Inheritance:

 

In the multilevel Inheritance, the child class became the parent of another child class.

Example:

class Base:

     def f1(self):

          print("this is first bas class")

class Base2(Base):

     def f2(self):

          print("This is second base class")

class Sub(Base2):

     def f3(self):

          print(" this is child funciton ")

obj = Sub()

obj.f1()

obj.f2()

obj.f2()

Hybrid Inheritance:

 

Use to inherit multiple inheritances from the same parent or base class.

Example:

class Base:

     def f1(self):

          print("this is first bas class")

class Base2(Base):

     def f2(self):

          print("This is second base class")

class Sub(Base):

     def f3(self):

          print(" this is child funciton ")

obj = Sub()

obj1 = Base2()

obj.f1()

obj1.f2()

obj.f3()

Hierarchical Inheritance:

 

Use to inherit multiple inheritances perform in single programs.

Example:

class Base:

     def f1(self):

          print("this is first bas class")

class Base2(Base):

     def f2(self):

          print("This is second base class")

class Sub(Base, Base2):

     def f3(self):

          print(" this is child funciton ")

obj = Sub()

obj1 = Base2()

obj.f1()

obj1.f2()

obj.f3()

Super() function

 

It allows us to call methods from the parent class.

Example:

class Base:

     def f1(self):

          print("this is first bas class")

class Sub(Base, Base2):

     def f3(self):

          Super().f1()

          print(" this is child funciton ")

obj = Sub()

obj.f1()

obj1.f2()

obj.f3()

Thanks for reading our tutorials, please do the comment in below section so we can modify it.

bottom of page