What is Method Overriding and how it is implemented in Python?

This Python tutorial helps to understand the concept of method overriding and how it is implemented in this programming language. First, we will learn what is method overriding in Python then we will go for its implementation.

Method Overriding in Python


Definition:-  Method Overriding is an Object Oriented Programming feature in which the subclass or child class implements a method which is already implemented in parent class. The method will get override in child class if the method name is given the same in the parent class. That means if you create an object of parent class and child class and execute both the objects created we will get a different output.

Advantage:- The main advantage of this feature is that the class can give it’s own defined model for an inherited method without changing the parent class code.

This is also useful when a parent class is having multiple child classes and one of the child class wants to use the parent class method, the child class can use the parent class method and parameters without any modification to the parent class code.


         Implementation of Method Overriding in Python

class Rectangle():
  def __init__(self,length,breadth):
    self.length = length
    self.breadth = breadth
  def getArea(self):
    print (self.length*self.breadth," is area of rectangle")
class Square(Rectangle):
  def __init__(self,side):
    self.side = side
  def getArea(self):
    print (self.side*self.side," is area of square")

r = Rectangle(3,5)
s = Square(10)



In the above code, two classes are created. The parent class in the above code is Rectangle and the child class in the above code is Square. The method in both classes is getArea(). Here,the child class getArea() method overrides the parent class getArea() method by giving different inputs.




15 is area of rectangle
100 is area of square

You can also read,

The concept of inheritance in Python and implementation

How to implement a Queue data structure in Python

Leave a Reply

Your email address will not be published. Required fields are marked *