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.

To override a method or perform method Overriding in Python Programming Language, 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 function in the Child with the same name and the same number of parameters.

For example, if you have the function that calculates 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 with an example.

Python Method Overriding Example

In this example, we created an employee class, which contains a message function that prints a message. Next, we created a department that inherits from the Employee class. Within this, we created a function 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.

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

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

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

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

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

print('------------')
sl = Sales()
sl.message()
This message is from Employee 
------------
This Department inherited from Employee
------------
This Sales is also inherited from Employee

Python Method Overriding in Multiple Inheritance

This Python example shows how to perform method overloading in multiple inheritance situations.

Refer to the Inheritance in Python.

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

Method overriding in multiple inheritance output

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

with arguments

Until now, we are only changing the print statement. I mean, overriding functions without any arguments. In this example, we created an add function with two arguments in the parent and three arguments in the 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 example, we are calling the parent function from the overriding method.

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

From the above screenshot, both give different results based on the object. Here, Employee.message(self) calls the print(‘This message is from Employee’) 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 method.

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