Inheritance In Python

Wajiha Urooj
Edureka
Published in
5 min readJul 19, 2019

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.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Python and Data Science.

1. Machine Learning Classifier in Python

2. Python Scikit-Learn Cheat Sheet

3. Machine Learning Tools

4. Python Libraries For Data Science And Machine Learning

5. Chatbot In Python

6. Python Collections

7. Python Modules

8. Python developer Skills

9. OOPs Interview Questions and Answers

10. Resume For A Python Developer

11. Exploratory Data Analysis In Python

12. Snake Game With Python’s Turtle Module

13. Python Developer Salary

14. Principal Component Analysis

15. Python vs C++

16. Scrapy Tutorial

17. Python SciPy

18. Least Squares Regression Method

19. Jupyter Notebook Cheat Sheet

20. Python Basics

21. Python Pattern Programs

22. Web Scraping With Python

23. Python Decorator

24. Python Spyder IDE

25. Mobile Applications Using Kivy In Python

26. Top 10 Best Books To Learn & Practice Python

27. Robot Framework With Python

28. Snake Game in Python using PyGame

29. Django Interview Questions and Answers

30. Top 10 Python Applications

31. Hash Tables and Hashmaps in Python

32. Python 3.8

33. Support Vector Machine

34. Python Tutorial

Originally published at https://www.edureka.co on July 19, 2019.

--

--