Inheritance in Python

Inheritance in Python 

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. The following are the concepts discussed in this article:

  1. What Is Inheritance?

2. init Function

3. Types Of Inheritance

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

4. Python super() Function

5. Python Method Overriding

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. The 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.

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 inheritances 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 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.

Compiled By: Pushpendra Maurya

Profession: Data Scientist

Source: Medium.com 

Comments

Popular posts from this blog

THE FUTURE OF AI IN EVERYDAY LIFE

Understanding The Difference Between AI, ML, And DL

The Impact of AI on the Education System: A Paradigm Shift