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-
- Using ClassName.method(self)
- 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
You may also like-