June 29, 2022

Method Overriding in Python With Examples

In this post we’’ll see how Method Overriding in Python works.

What is Method Overriding

Method overriding is an object oriented programming concept which states that a child class can provide a different implementation of a method that is already there in one of its parent classes. If a method in child class has the same name, same number of arguments and the same return type as in parent class then the child class is said to be overriding the method of the parent class.

Python Method Overriding example

In the example there is a hierarchy where parent class Person has a method display_info(). Child class Employee overrides the method display_info() and provides its own implementation.

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

  def display_info(self):
    print('In display_info method of Person class')
    print('Name:', self.name)
    print('Age:', self.age)


class Employee(Person):
  def __init__(self, emp_id, department, name, age):
    super().__init__(name, age)
    self.emp_id = emp_id
    self.department = department

  def display_info(self):
    Person.display_info(self)
    print('In display_info method of Employee class')
    print('Id:', self.emp_id)
    print('Department:', self.department)


e = Employee(1, "IT", "Michael Weyman", 42)
e.display_info()
p = Person("Michael Weyman", 42)
p.display_info()
Output
In display_info method of Person class
Name: Michael Weyman
Age: 42
In display_info method of Employee class
Id: 1
Department: IT
In display_info method of Person class
Name: Michael Weyman
Age: 42

As you can see when we have an object of Employee class, display_info() method of the Employee class (child class) is called. When object is of Person class, display_info() method of the Person class (Parent class is called).

Calling parent class’ method from overridden method

You can call the method of the parent class from the child class, if you want that. One way to do that is by using the class name- CLASS_NAME.method_name(self). This way is already used in the above example.

Another and better way is to use super() function in Python to call the method of the super class.

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

  def display_info(self):
    print('In display_info method of Person class')
    print('Name:', self.name)
    print('Age:', self.age)


class Employee(Person):
  def __init__(self, emp_id, department, name, age):
    super().__init__(name, age)
    self.emp_id = emp_id
    self.department = department

  def display_info(self):
    super().display_info()
    print('In display_info method of Employee class')
    print('Id:', self.emp_id)
    print('Department:', self.department)


e = Employee(1, "IT", "Michael Weyman", 42)
e.display_info()
Output
In display_info method of Person class
Name: Michael Weyman
Age: 42
In display_info method of Employee class
Id: 1
Department: IT

In the example you can see the use of super() to call the parent class method.

That's all for the topic Method Overriding in Python With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment