Python Method Overriding

Posted by

Python method overriding is a similar concept like we do in terms of Method Overloading. Here we make use of a method with the same name multiple times in multiple classes.

If you want to alter the behaviour of the method according to your requirement yes it’s possible i.e., you don’t want to make use of parent class implementation.

Here you will have the freedom to override the method functionality with the requirement in the child class specified.

Method overriding is a feature of object oriented programming that allows a subclass to provide a specific implementation of a method that is already defined in its superclass.

When a method in a subclass has the same name, return type, and parameter list as a method in its superclass, the method in the subclass overrides the method in the superclass.

When an object of the subclass calls the overridden method, the specific implementation in the subclass is executed instead of the implementation in the superclass. This allows subclasses to provide a specialized implementation of a method inherited from its superclass, while still retaining the same interface.

To override a method in a subclass, the method in the subclass must have the same signature (name, return type, and parameter list) as the method in the superclass. Additionally, the access level of the method in the subclass cannot be more restrictive than the access level of the method in the superclass.

This concept also makes it flexible for you to perform code reuse which will have a lot of advantages like faster processing, less memory consumption and much more…

Python Method Overriding Video Tutorial :

Go through the below tutorial for more detailed explanation of Method Overriding.

Create a class A and add a print statement in method display

Class A:
    def display():
        print("I am class A")

Let’s try to create an object for it and print the data

c = A()
c.display()

output:

I am class A

Now let us create a new class B so that we can know the implementation of Method Overriding

Class B:
    a = 10

Create a object for class B. Now try to call the method display here.

c = B()
c.display()

You will get a error because display method is not there in class B

Now inherit class A on class B

Class B(A):
    a = 10

Now try to call display method which is intern extended by class A

c = B()
c.display()

output: We can observe display method is printed which was declared in class A.

I am class A

Here we are making use of the inheritance concept and providing method overriding functionality.

And now what if i want to change the message in class B that is to be printed ??

Class B(A):
    def display():
        print("I am class B")

make use of the object and call display

c = B()
c.display()

here though we are inheriting class A but we do have a display method in class B so we consider it.

output:

I am class B

Can we inherit multiple classes and fetch their methods ???

I have clearly explained in the video tutorial so suggest you to go through it.


If you have any query’s in this implementation of python method overriding do let us know in the comment section below. If you like this tutorial do like and share us for more interesting updates.