Method Overriding in Python

In python, method overriding means overriding the base class method in child class by creating the method with same name and signature to perform different operations.

 

As discussed in python inheritance, you can inherit the base class methods and use them in child classes. If required, you can also modify the functionality of base class methods in child class by creating the methods with the same name and signature to perform different tasks.

Python Method Overriding Example

Following is an example of implementing the method overriding in python.

 

#Base Class
class User:

   def getinfo(self):
     print("Learn Python")

#Derived Class
class Employees(User):

   def getinfo(self):
     print("Welcome to Tutlane")

e1 = Employees()
e1.getinfo()

If you observe the above example, the base class (User) method (getinfo) has overridden in the child class (Employees) by creating the method (getinfo) with the same name and signature.

 

The above python method overriding example will return the result as shown below.

 

Welcome to Tutlane

This is how you can implement the method overriding in python based on your requirements.