When a method is defined in a class and also in one of its sub classes, then it is called as Method overriding in Python.

Let’s dive directly into the example for method overriding, Assume, we have two classes as Account  (parent class) and SavingsAccount  (sub class of Account) as mentioned below,

class Account:

    def __init__(self, account_number, account_holder_name):
        self.account_number = account_number
        self.account_holder_name = account_holder_name
        print('Initialized Account class')

    def rate_of_interest(self):
        roi = 5
        print("Rate of Interest for this account - {} is {}".format(self.account_number, roi))
        return roi
class SavingsAccount(Account):

    def __init__(self, account_number, account_holder_name, account_type):
        super().__init__(account_number, account_holder_name)
        self.account_type = account_type

    def rate_of_interest(self):
        roi = 6
        print("Rate of Interest for this account - {} is {}".format(self.account_number, roi))
        return roi

In above example, both the classes have a common method as rate_of_interest().

Let’s execute below code to get some interesting result based on instance of above classes,

acct = Account('111111111', 'AccountOne')
acct.rate_of_interest()

print('------------------------------------------')

savings_acct = SavingsAccount('222222222', 'AccountTwo', 'savings')
savings_acct.rate_of_interest()

Output of above code snippet will be as below,

Initialized Account class
Rate of Interest for this account - 111111111 is 5
------------------------------------------
Initialized Account class
Rate of Interest for this account - 222222222 is 6

In above code sample, rate_of_interest() method is overridden in SavingsAccount class. When rate_of_interest() is called on Account instance, the method return roi as 5, in case of SavingsAccount instance, we get roi as 6.

method overriding in python

Whenever a method is called using sub class instance, Python will look for that method in sub class, if the method is not found in sub class, it will continue to look for that method in parent class. This way of implementing method of same name in multiple class hierarchy is called method overriding in Python.

Always remember, the method available in last created sub class will be executed first if it is overridden.

The Built-in Object Class

The class Object is the base (root) class for all classes in Python.

It has methods that are therefore available in all Python objects. It defines a common set of special methods and intrinsic attributes. The methods include the special methods __str__(), __init()__, __eq__() (equals) and __hash__() (hash method). It also defines attributes such as __class__, __dict__, __doc__ and __module__.

All the method mentioned in object class can be overridden in any class of Python.

Use of Method Overriding

  1. Method overriding is used to provide the specific implementation of a method which is already provided by its super class.
  2. By using method overriding, the behavior of parent class method can be inherited which results in code minimization and also the behavior can be enhanced or customized more specific to that class type.

Conclusion

In this article, we saw about implementation, behavior and usage of method overriding in Python.