Day #74 - Method Overriding in Python

Day #74 - Method Overriding in Python

ยท

4 min read

Introduction

Welcome to my 74th blog post on the Python journey. On day 74, I learned about a powerful feature of python i.e method overriding. It allows us to redefine a method in a derived class. Let's dive into more details and understand how to override the methods and uses of method overriding in Python.

So let's get started......

Method Overriding in Python

  • It allows us to redefine a method in a derived class. When we redefine the method in the derived class it is said to override the method in the base class.

  • To execute the version of the overridden method we create an instance of the derived class and call the overridden method. Due to this overridden method is called, rather than the version in the base class.

  • So method overriding is a way to customize the behavior of a class based on its specific needs.

Example -

Consider the base class -

class Shape:
    def area(self):
        pass

Here the area method is defined but does not have any implementation. Now if we create a derived class that represent a circle or rectangle or triangle, we can overide the area method from the shape class and provide the implementation to it. Below is the example for the same -

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius * self.radius

In the above example, the circle class inherits the shape class and overrides the area method and provides the implementation of the area method to calculate the circle area, based on the given radius.

Note - When a method is overridden, the new implementation must have the same method signature as the original method i.e the number and type of arguments, as well as the return type, must be the same.

Overriding using 'super'

  • Another way to override methods is to use the 'super' Keyword.

  • The super function allows you to call the base class method from the derived class method, and use it when you want to extend the behavior of the base class method, rather than replace it.

Example -

consider the below base class

class Shape:
    def area(self):
        print("Calculating area...")

Here the base class shape uses the area method to print message indicating that the area is being calculated. If we want to create a derived class that represents a circle, and we print a message indicating the type of shape, we make use of the super function to call the base class and add our customized message.

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        print("Calculating area of a circle...")
        super().area()
        return 3.14 * self.radius * self.radius

Here the Circle class

  • overrides the area method,

  • calls the base class method using the super function

  • this allows us to extend the behavior of the base class method, while still maintaining its original behavior.

More Example

class Shape:
  def __init__(self, x, y):
    self.x = x
    self.y = y

  def area(self):
      return self.x * self.y

class Circle(Shape):
    def __init__(self, radius):
      self.radius = radius
      super().__init__(radius, radius)

    def area(self):
        return 3.14 *  super().area()

# rec = Shape(3, 5)
# print(rec.area())

c = Circle(5)
print(c.area())

Output

78.5

Resources Used

You can watch the video of Day#74 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. On day 74, I learned about the method overriding in Python. It allows us to customize the behavior of a class based on its specific needs. Using method overriding, we can write more robust and reliable code, while ensuring clear readability and functioning of our classes. Using method overriding we can extend class behavior rather than replacing it, giving more flexibility and control over class behavior.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #74

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย