Sunday, June 5, 2022

Method Overriding in Python

Method overriding is an OOPS concept which provides ability to change the implementation of a method in a child class which is already defined in one of its super class. If there is a method in a super class and method having the same name and same number of arguments in a child class then the child class method is said to be overriding the parent class method.

Method overriding helps in hierarchical ordering where we move from general implementation in the super class to more specific implementation in the child classes.


Method overriding Python example

In the example there is a class Person with fields name and age and a child class Employee with an additional field empId. There is a method displayData in the Person class to display value of the fields. In the Employee class that method is overridden to display empId too.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def displayData(self):
    print('In parent class displayData method')
    print(self.name)
    print(self.age)

class Employee(Person):
  def __init__(self, name, age, id):
    # calling constructor of super class
    super().__init__(name, age)
    self.empId = id

  def displayData(self):
    print('In child class displayData method')
    print(self.name)
    print(self.age)
    print(self.empId)

#Person class object
person = Person('John', 40)
person.displayData()
#Employee class object
emp = Employee('John', 40, 'E005')
emp.displayData()

Output

In parent class displayData method
John
40
In child class displayData method
John
40
E005

In the example when the displayData() method is called with Person class object, displayData() of the Person class is executed. When displayData() method is called with Employee class object, displayData() of the Employee class is executed. So the appropriate overridden method is called based on the object type, which is an example of Polymorphism.

Calling parent class overridden method from the child class

In the above example in the displayData() method of the child class same fields are printed again causing redundancy. It would be better to call the parent class displayData() method for printing name and age and print empId in the displayData() method of the child class.

To call the overridden method of the super class you can use of the following ways-

  1. Using ClassName.method(self)
  2. Using super()

Calling super class method using class name

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def displayData(self):
    print('In parent class displayData method')
    print(self.name)
    print(self.age)

class Employee(Person):
  def __init__(self, name, age, id):
    # calling constructor of super class
    super().__init__(name, age)
    self.empId = id

  def displayData(self):
    print('In child class displayData method')
    Person.displayData(self)
    print(self.empId)

#Employee class object
emp = Employee('John', 40, 'E005')
emp.displayData()

Output

In child class displayData method
In parent class displayData method
John
40
E005

Calling super class method using super()

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def displayData(self):
    print('In parent class displayData method')
    print(self.name)
    print(self.age)

class Employee(Person):
  def __init__(self, name, age, id):
    # calling constructor of super class
    super().__init__(name, age)
    self.empId = id

  def displayData(self):
    print('In child class displayData method')
    #calling super class method
    super().displayData()
    print(self.empId)

#Employee class object
emp = Employee('John', 40, 'E005')
emp.displayData()

Output

In child class displayData method
In parent class displayData method
John
40
E005

That's all for this topic Method Overriding in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Inheritance in Python
  2. Method Overloading in Python
  3. Name Mangling in Python
  4. Interface in Python
  5. Python break Statement With Examples

You may also like-

  1. Constructor in Python - __init__() function
  2. Python count() method - Counting Substrings
  3. Method Overriding in Java
  4. Difference Between Abstract Class And Interface in Java
  5. How HashSet Works Internally in Java
  6. Installing Hadoop on a Single Node Cluster in Pseudo-Distributed Mode
  7. Sending Email Using Spring Framework Example
  8. Dependency Injection in Spring Framework