Python Programming (138 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (32 Blogs)
  • Data Science (26 Blogs)
  • Mastering Python (92 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

Inheritance In Python With Examples: All You Need To Know

Last updated on Jul 15,2021 57.1K Views

13 / 62 Blog from Python Fundamentals

Python programming language is easy to learn and works on both procedural and object oriented programming approach. Inheritance is one such concept in object oriented programming. Code reusability being the forte of inheritance, it helps in a lot of applications when we are working on Python. Following are the concepts discussed in this article:

What Is Inheritance?

The method of inheriting the properties of parent class into a child class is known as inheritance. It is an OOP concept. Following are the benefits of inheritance.

  1. Code reusability- we do not have to write the same code again and again, we can just inherit the properties we need in a child class.

  2. It represents a real world relationship between parent class and child class.

  3. It is transitive in nature. If a child class inherits properties from a parent class, then all other sub-classes of the child class will also inherit the properties of the parent class.

Below is a simple example of inheritance in python.


class Parent():
       def first(self):
           print('first function')

class Child(Parent):
       def second(self):
          print('second function')

ob = Child()
ob.first()
ob.second()

Output: 
first function
second function

In the above program, you can access the parent class function using the child class object.

Sub-classing 

Calling a constructor of the parent class by mentioning the parent class name in the declaration of the child class is known as sub-classing. A child class identifies its parent class by sub-classing.

__init__( ) Function

The __init__() function is called every time a class is being used to make an object. When we add the __init__() function in a parent class, the child class will no longer be able to inherit the parent class’s __init__() function. The child’s class __init__() function overrides the parent class’s __init__() function.

class Parent:
     def __init__(self , fname, fage):
          self.firstname = fname
          self.age = fage
     def view(self):
         print(self.firstname , self.age)
class Child(Parent):
     def __init__(self , fname , fage):
          Parent.__init__(self, fname, fage)
          self.lastname = "edureka"
     def view(self):
          print("course name" , self.firstname ,"first came",  self.age , " years ago." , self.lastname, " has courses to master python")
ob = Child("Python" , '28')
ob.view()
 

Types Of Inheritance

Depending upon the number of child and parent classes involved, there are four types of inheritance in python.

Types of inheritance- inheritance in python-edureka

Single Inheritance

When a child class inherits only a single parent class.

class Parent:
     def func1(self):
          print("this is function one")
class Child(Parent):
     def func2(self):
          print(" this is function 2 ")
ob = Child()
ob.func1()
ob.func2()

Multiple Inheritance

When a child class inherits from more than one parent class.

class Parent:
   def func1(self):
        print("this is function 1")
class Parent2:
   def func2(self):
        print("this is function 2")
class Child(Parent , Parent2):
    def func3(self):
        print("this is function 3")

ob = Child()
ob.func1()
ob.func2()
ob.func3()

Multilevel Inheritance

When a child class becomes a parent class for another child class.

class Parent:
      def func1(self):
          print("this is function 1")
class Child(Parent):
      def func2(self):
          print("this is function 2")
class Child2(Child):
      def func3("this is function 3")
ob = Child2()
ob.func1()
ob.func2()
ob.func3()

Hierarchical Inheritance

Hierarchical inheritance involves multiple inheritance from the same base or parent class.

 class Parent:
      def func1(self):
          print("this is function 1")
class Child(Parent):
      def func2(self):
          print("this is function 2")
class Child2(Parent):
      def func3(self):
          print("this is function 3")

ob = Child()
ob1 = Child2()
ob.func1()
ob.func2()

Hybrid Inheritance

Hybrid inheritance involves multiple inheritance taking place in a single program.


class Parent:
     def func1(self):
         print("this is function one")

class Child(Parent):
     def func2(self):
         print("this is function 2")

class Child1(Parent):
     def func3(self):
         print(" this is function 3"):

class Child3(Parent , Child1):
     def func4(self):
         print(" this is function 4")

ob = Child3()
ob.func1()

Python Super() Function

Super function allows us to call a method from the parent class.


class Parent:
     def func1(self):
         print("this is function 1")
class Child(Parent):
     def func2(self):
          Super().func1()
          print("this is function 2")

ob = Child()
ob.func2()

Python Method Overriding

Method Overriding

You can override a method in python. Look at the example below.


class Parent:
    def func1(self):
        print("this is parent function")

class Child(Parent):
    def func1(self):
        print("this is child function")

ob = Child()
ob.func1()

The functionality of the parent class method is changes by overriding the same method in the child class.

Inheritance is one of the most important concept of OOP. It provides code reusability, readability and transition of properties which helps in optimized and efficient code building. Python programming language is loaded with concepts like inheritance. Enormous python applications calls for increased number of python programmers in the recent market. To master your skills and kick-start your learning enroll to Edureka’s python certification program and become a python developer in no time.

Have any questions? mention them in the comments section. Our team will get back to you as soon as possible.

 

Upcoming Batches For Python Certification Training Course
Course NameDate
Python Certification Training Course

Class Starts on 28th January,2023

28th January

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 25th February,2023

25th February

SAT&SUN (Weekend Batch)
View Details
Python Certification Training Course

Class Starts on 25th March,2023

25th March

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Inheritance In Python With Examples: All You Need To Know

edureka.co