Method Overriding in Python

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()
This message is from Employee Class
------------
This Department class is inherited from Employee

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 inherited from Employee')
 

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

print('------------')
sl = Sales()
sl.message()

Method Overriding example output

This message is from Employee Class
------------
This Department class inherited from Employee
------------
This Sales class is also inherited from Employee

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

Python method overriding in multiple inheritance output

This message is from Employee Class
------------
This Department class is inherited from Employee
------------
This Sales class is inherited from Employee

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()
This message is from Employee Class
------------
This message is from Employee Class
This Department class is inherited from Employee

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()
This message is from Employee Class
------------
This message is from Employee Class
This Department class is inherited from Employee