Inheritance In Python With Examples

In this article, we discuss Inheritance In Python With Examples

Inheritance In Python With Examples

Inheritance In Python With Examples
Inheritance In Python With Examples

Inheritance In Python With Examples

Python programming language is easy to learn and works on both procedural object-oriented programming and approaches. 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.

the concepts discussed in this article

  • What Is Inheritance?
  • init Function
  • Types Of Inheritance
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
  • Python super() Function
  • Python Method Overriding

What Is Inheritance?

The method of inheriting the properties of the 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()

Inheritance In Python With Examples

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.

image 13
Inheritance In Python With Examples

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()

Inheritance In Python With Examples

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()

Inheritance In Python With Examples

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 changed by overriding the same method in the child class.

Inheritance is one of the most important concepts 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 an inheritance. Enormous python applications call for an increased number of python programmers in the recent market.

we have come to the end of our article. I hope you understood Inheritance In Python With Examples

Source: Various

Inheritance In Python With Examples

Also Check out What is Method Overloading in Python

If you intreasted in Entertainment genre MOVIES STREAMING DATES

Related Posts

One thought on “Inheritance In Python With Examples

  1. Hello, after reading this awesome article i am too glad to share my experience here with friends.

Leave a Reply

Your email address will not be published. Required fields are marked *