Inheritance in Python with Examples

In this article, We are going to cover Inheritance in Python with Examples.

What is Inheritance in Python ?

It is a process where we inherit forward properties and methods of parent (base) to child class (derived).

It provides reusability of a code. No need to write the same code again and again.

Syntax of Inheritance in Python:

class Parent:
	# statements
class child(Parent):
	# statements 

Example of Inheritance in Python:

class Teacher: 
    def __init__(self, name): 
        self.name = name 
    def getName(self): 
        return self.name 
    # To check if this person is a student 
    def isStudent(self): 
        return False
# Inherited  
class Student(Teacher): 
   def isStudent(self): 
        return True
stud = Teacher("Lily")   
print(stud.getName(), stud.isStudent()) 
stud = Student("Lucky") 
print(stud.getName(), stud.isStudent())

Output:

Lily False
Lucky True
Inheritance in Python with Examples 1

Types of Inheritance in Python:

  1. Single inheritance:

In Python Single Inheritance, a derived class is derived only from a single parent class and allows class to derive behaviour and properties from a single base class. Thus enables code reusability.

Syntax of Single Inheritance in Python:

class Parent:
	# statement
class Child:
	# statement 

Example of Single Inheritance in python:

class Parent:
     def myfun_p(self):
          print("Calling Parent Class")
class Child(Parent):
     def myfun_c(self):
          print("Calling Child Class")
obj = Child()
obj.myfun_p()
obj.myfun_c()

Output:

Calling Parent Class
Calling Child Class
Inheritance in Python with Examples 2

2. Multiple Inheritance:

In Python, class can be derived from more than one base class it is called multiple inheritance. In multiple inheritance, all the features of the base classes are inherited into the derived class.

Syntax of Multiple Inheritance in Python:

class Parent1:
	# statements
class  Parent2:
	# statements
class multi_child(Parent1, Parent2):
	# statements

  Example of multiple inheritance:

class Name:
    name = ""
    def myfun1(self):
        print(self.name)
class Surname:
    surname = ""
    def myfun2(self):
        print(self.surname)
# Multi Child class
class Student(Name, Surname):
    def parents(self):
        print("Name :", self.name)
        print("Surname :", self.surname)
stud = Student()
stud.name = "Lily"
stud.surname = "Jones"
stud.parents()

Output:

Name : Lily
Surname : Jones
Inheritance in Python with Examples 3

3. Multilevel Inheritance:

Multilevel Inheritance is archived when a derived class inherits another derived class. There is no limit on the number of levels.

Syntax of Multilevel Inheritance in Python:

class Parent:
	# statements
class Parent1(Parent):
	# statements
class Child(Parent1):
	# statements 

Example of Multilevel Inheritance in Python:

class Principal:  
    def principal(self):  
        print("I'm Principal")  
class Teacher(Principal):  
    def teacher(self):  
        print("I'm Teacher")   
class Student(Teacher):  
    def student(self):  
        print("I'm student")  
d = Student()  
d.principal()  
d.teacher()  
d.student()

Output:

I'm Principal
I'm Teacher
I'm student
Inheritance in Python with Examples 4

Conclusion:

We have covered Inheritance in Python with Examples

Related Articles:

Functions in Python with Examples

Loops in Python 3 with Examples

7 Python Operators with Examples

Python Introduction for Programmers [Part 1]

6 Python Conditional Statements with Examples

Encapsulation and Polymorphism in Python with Examples

Reference:

Python official Page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern at startup Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link