Python

Python Method Overriding

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.

Abhishek

Share
Published by
Abhishek

Recent Posts

5G and 6G Technology Advancements: A Promising Future of Connectivity

5G and 6G Technology AdvancementsWhat is 5G, and How Has It Transformed Connectivity?1. Understanding 5G…

2 months ago

HTML: The Evolution and Power of Unleashed Web Language and Modern too

IntroductionInitial StagesEvolutionChallenging Other LanguagesCurrent TrendsAI and HTMLConclusion Introduction HTML, or HyperText Markup Language, is the…

5 months ago

Increase in 80C in Budget 2024 ?

Increase in 80CDemands and Discussions: Increase in 80C Section 80C of the Income Tax Act…

5 months ago

ChatGPT 4o Unleashing the Power of GPT A Comprehensive Guide

IntroductionWhat is ChatGPT-4?Key Features of ChatGPT-4Enhanced Natural Language UnderstandingImproved Response GenerationVersatilityApplications of ChatGPT-4Customer SupportContent CreationEducational…

8 months ago

APJ Abdul Kalam Biography: A Tribute to India’s Powerful Missile Man

APJ Abdul Kalam Biography :Childhood :Academics :Professional Career :Achievements : APJ Abdul Kalam Biography :…

10 months ago

Srinivasa Ramanujan Biography: Exploring the Genius

We value your feedback! Please share your thoughts on this blog on Srinivasa Ramanujan Biography…

11 months ago

This website uses cookies.