Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Method Overriding in Python

by suresh

The method overriding in Python means creating two methods with the same name but differ in the programming logic. The concept of Method overriding allows us to change or override the Parent Class function in the Child Class.

In Python, to override a method, you have to meet certain conditions, and they are: 

  • You can’t override a method within the same class. It means you have to do it in the child class using the Inheritance concept.
  • To override the Parent Class method, you have to create a method in the Child class with the same name and the same number of parameters.

For example, if you have the function that calculated the Salary Hike for all the employees in an Organisation. But particular departments or people get a different percentage. In this scenario, you can override the existing method in the Department class and write your logic.

In this section, we discuss how to perform Method Overriding in Python Programming Language with an example.

Python Method Overriding Example

In this example, we created an employee class, which contains a message method that prints a message. Next, we created a department class that inherits from Employee class. Within this class, we created a method with the same name message with a different print message. 

It is a simple demonstration of Python Method Overriding. Here, we are just overriding the message. Next, we created an Object for Employee and Department class and calling that message function.

# Python Method Overriding
 
class Employee:
      
    def message(self):
        print('This message is from Employee Class')
  
class Department(Employee):
  
    def message(self):
        print('This Department class is inherited from Employee')
  
emp = Employee()
emp.message()
  
print('------------')
dept = Department()
dept.message()
Method Overriding in Python 1

As you can see, the emp object is printing a string from the Employee class message function. Whereas, dept.message() is a printing test from Department class.

In this Python Method Overriding example, we created one more class that inherited from Employee class. Next, we are overriding the message function from this Sales class as well. Please refer Python class example.

class Employee:
      
    def message(self):
        print('This message is from Employee Class')
  
class Department(Employee):
  
    def message(self):
        print('This Department class is inherited from Employee')
 

class Sales(Employee):
  
    def message(self):
        print('This Sales class is inherited from Employee')
         
emp = Employee()
emp.message()
  
print('------------')
dept = Department()
dept.message()
 

print('------------')
sl = Sales()
sl.message()
Method Overriding in Python 2

Python Method Overriding in Multiple Inheritance

This Python example shows how to perform method overloading in multiple inheritance situation. Refer to the Python Inheritance in Python.

class Employee:
      
    def message(self):
        print('This message is from Employee Class')
  
class Department(Employee):
  
    def message(self):
        print('This Department class is inherited from Employee')
 
class Sales(Department):
  
    def message(self):
        print('This Sales class is inherited from Employee')
         
emp = Employee()
emp.message()
  
print('------------')
dept = Department()
dept.message()
 
print('------------')
sl = Sales()
sl.message()
Method Overriding in Python 3

Python Method Overriding with arguments

Until now, we are only changing the print statement. I mean, overriding methods without any arguments. In this example, we created an add method with two arguments in the parent class and three arguments in child class. 

class Employee:
      
    def add(self, a, b):
        print('The Sum of Two = ', a + b)
  
class Department(Employee):
  
    def add(self, a, b, c):
        print('The Sum of Three = ', a + b + c)
         
emp = Employee()
emp.add(10, 20)
  
print('------------')
dept = Department()
dept.add(50, 130, 90)
Method Overriding in Python 4

Calling Parent Function within the Method Overriding in Python

In this Python method overriding example, we are calling the parent function from the overriding method.

class Employee:
      
    def message(self):
        print('This message is from Employee Class')
  
class Department(Employee):
  
    def message(self):
        Employee.message(self)
        print('This Department class is inherited from Employee')
  
emp = Employee()
emp.message()
  
print('------------')
dept = Department()
dept.message()
Method Overriding in Python 5

From the above screenshot, both are giving different results based on the object. Here, Employee.message(self) calls the print(‘This message is from Employee Class’) statement and prints the message within that print function. Instead of using the class name, you can use the super() function to call the parent class method.

class Employee:
      
    def message(self):
        print('This message is from Employee Class')
  
class Department(Employee):
  
    def message(self):
        super().message()
        print('This Department class is inherited from Employee')
  
emp = Employee()
emp.message()
  
print('------------')
dept = Department()
dept.message()
Method Overriding in Python 6

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy