Page Contents
In this tutorial we will learn about Method Overriding in Python . Before reading this tutorial we recommended to read about below tutorials –
- Object Oriented Programming in Python – An Intro for Beginners
- Inheritance in Python – A Detailed Explanation with Examples
What is Method Overriding in Python
Suppose a method is defined in a base class and when we define same named method in the derived class derived from that base class, then it is called Method Overriding.
If the method is called by creating an object of the base class then the method of the base class will run and if the method is called by creating an object of the derived class then the method of the derived class will run .
If there are many derived classes and each derived class has a method with the same name, then we can clearly understand with the help of Method resolution order that if we create an object of any class and call the method, the method of which class will run.
Now with the help of examples we will clearly understand the concept of Method overriding in Python.
Examples of Method Overriding in Python
Example 1: Override any object method in Python
# defining base class Employee
class Employee:
def details(self):
print("method is called from Employee class")
# Defining derived class Programmer
class Programmer(Employee):
def details(self):
print("method is called from Programmer class")
In the above code we have defined a base class Employee
and a derived class of the Employee
class Programmer
. Both base class and derived class has same named object method that is details()
. So in Programmer class details()
method is overridden .
# creating an object of Employee
employee1=Employee()
# calling details method
employee1.details()
method is called from Employee class
Then we have created an object of the Employee
class named employee1
and called the details()
method . As expected details()
method from Employee
class has been called and printed method is called from Employee class
in the output .
# creating an object of Programmer
programmer1=Programmer()
# calling details method
programmer1.details()
method is called from Programmer class
After that we have created an object of the Programmer
class named programmer1
and called the details()
method . As program has got details()
method in the Programmer
class itself so details()
method from Programmer
class has been called and printed method is called from Programmer class
in the output .
Example 2: Override __init__ Python
# define a base class
class Student:
def __init__ (self, name, age):
self.name = name
self.age = age
In the above code, a base class named Student
is defined which have a __init__
method ..
# define derived class1
class Arts_student(Student):
def __init__ (self, name, age, subject, roll):
super().__init__(name,age)
self.subject = subject
self.roll_no = roll
# define derived class2
class Science_student(Student):
def __init__ (self,name,age,subject):
super().__init__(name,age)
self.subject = subject
Then two derived classes are defined as Arts_student
and Science_student
each of which has inherited the Student
base class. Each of them have a __init__
method . So we have override _init__
method in both the derived classes
# details of Student class
print(Student.mro())
rajkumar = Student("Rajkumar", 27)
print(rajkumar.name)
print(rajkumar.age)
[<class '__main__.Student'>, <class 'object'>] Rajkumar 27
Then the method resolution order of the Student
class has been checked with the help of mro()
function. Since the Student
class is first in the list returned by the mro()
function, then when the object of the Student
is created then the __init__
method of the Student
is called. Then the object variable of the generated object is printed.
# details of Arts_student class
print(Arts_student.mro())
sudipta = Arts_student("Sudipta", 28, "history", 2)
print(sudipta.name)
print(sudipta.age)
print(sudipta.subject)
print(sudipta.roll_no)
[<class '__main__.Arts_student'>, <class '__main__.Student'>, <class 'object'>] Sudipta 28 history 2
Then the method resolution order of Arts_student
class has been checked with the help of mro()
function. Since the Arts_student
class is is in the first position of the list returned by the mro()
function, then the __init__
method of Arts_student
is called when the object of Arts_student
is created. Then the object variable of the generated object is printed. Here __init__
method of Arts_student
overrides the __init__
method of Student
.
# details of Science_student_class
print(Science_student.mro())
sourav = Science_student("Sourav", 29, "math")
print(sourav.name)
print(sourav.age)
print(sourav.subject)
[<class '__main__.Science_student'>, <class '__main__.Student'>, <class 'object'>] Sourav 29 math
Then the method resolution order of Science_student
class has been checked with the help of mro()
function. Since Science_student
class is first in the list returned by mro()
function, then when Science_student
‘s object has been created, Science_student
‘s __init__
method has been called. Then the object variable of the generated object is printed. Here __init__
method of Science_student
overrides the __init__
method of Student
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .