Python Programming - Method Overriding in Python

Python Programming – Method Overriding in Python

Method overriding is one of the important concepts to consider while using inheritance in Python. As we see in the previous programming Code 11.1. for single inheritance that the __init__() method was defined both in the base class as well as derived class. When an object of a derived class is created then the __init_() method of derived class overrides that of the base class. That means, the __init__( ) of the derived class rectangle takes preference over the __init__( ) method of shape class. In order to overcome this issue, the __init__() method of the derived class is extended further to make a call to the __init__( ) method of base class. This is done by using shape, __init__() as shown in code 11.1. By invoking this way, a call to the__init__() method of shape class is made from the __init__() method of the derived class rectangle.

Method overriding can also be resolved by using a built-in Python function super(). The super() function invokes the parent constructor method by itself without referring to the class name as mentioned in the call shape.__init__(). The super() function is used as follows:

super().__init__()

The programming illustration of resolving method overriding issue is given in Code 11.4. In this code, the ambiguity in single inheritance is resolved by using the super().__init__() function as highlighted in bold in the code rather than using the shape.__init__(self) method. It is to be noted here that while calling the constructor method using the super() function the__init__() function does not contain the argument self.

Code: 11.4. Illustration of built-in function super() for single inheritance.

#illustration of resolving method overriding using super()

class shape:                          #base class
‘A shape class’
def__init__(self):                      #constructor method of base class
self, length = float(input(‘Enter Length:’))
self, breadth = float(input(‘Enter Breadth:’))

def display(self):                        #display method of base class
print (“\nLength:”, self.length)
print (“\nBreadth: “, self.breadth)

class rectangle(shape):                        #derived class
‘A rectangle class’
def__init__(self):                           #constructor method of derived class
super().__mit__0
self.area=0
def compute_area(self):                     #compute_srea funciton of derived class
self, area = self. length*self. breadth
print(“\nArea of rectangle-‘, self, area)

rl=rectangle() # object of derived class
rl.display( )
rl ,compute_area()

Output

Enter Length: 12
Enter Breadth:34
Length: 12.0
Breadth: 34.0
Area of rectangle= 408.0

In the case of multilevel inheritance, the super() function is used as shown in Code 11.5. In this program, we see that while creating the derived class object rl, the initial call is made to__init__() method of the derived class result. Which further invokes the __init__() method of its parent class marks by using the super() function. Furthermore, the__init__() method of marks class invokes its superclass student’s __init__() method by using the super() function again. The statement code comprising the super() function is highlighted in bold.

Code: 11.5. Illustration of built-in function super() for multilevel inheritance.

#Illustration of super() function in multilevel inheritance

class student: #base class
‘A student class’
def __init__(self): #constructor method of base class
self.name = (input(‘Enter Student Name:’))
self.rollno = int(input(‘Enter RollNo:’))

defdisplay(self): #display method of
base class
print (“\nName:”, self.name)
print (“\nRollNo: “, self.rollno)

class marks(student): #derived class
‘A marks class’
def __init__(self): ‘ #constructor method of derived class
super(). __init__( )
self.ml = float(input(‘Enter marks of subject 1:’))
self.m2 = float(input(‘Enter marks of subject 2:’))
self.m3 = float(input(‘Enter marks of subject 3:’))
def displaymarks(self): #display function of base class 2
print(“\nMarks of subject 1=”, self.m1)
print(“\nMarks of subject 2=”, self.m2)
print(“\nMarks of subject 3=”, self.m3)

class result(marks): # new derived class
‘A result class’
def__init__(self): #Constructor method of new derived class
# student. __init__(self)
# marks.__init__(self)
super( ). __init__( )
self.totalmarks = self.ml + self.m2 + self.m3
self.perc = self.total_marks*100/300
def display_result(self): #display_result function of
#derived class
print(“\nTotal Marks:”, self.total_marks)
print(“\nPercentage:”, self.perc)

rl=result() # object of derived class result
rl .display( )
rl .display_marks()
rl.display_result()

Output

Enter Student Name:Robin
Enter RollNo:222
Enter marks of subject 1:34
Enter marks of subject 2:54
Enter marks of subject 3:5 5

Name: Robin

RollNo: 222

Marks of subject 1= 34.0

Marks of subject 2= 54.0

Marks of subject 3= 55.0

Total Marks: 143.0

Percentage: 47.666666666666664

In the case of multiple inheritances, the super() function is used as presented in Code 11.6. In this code, we see that as there exists two parent classes student and marks. Therefore, while invoking __init__ ( )method using super() function only one of the parent’s constructor will be invoked, actually the one which is inherited first by the derived class. For example, the__init__() method of student class will be invoked by using the super() function. In order to make a call to the__init__() method of other parent class marks, we need to use the previous method, which is marks.__init__(self). Therefore, it is to be noted here that in the case of multiple inheritances, the super() function invokes the__init__() constructor method of the firstly inherited parent class and make the call to other
parents classes the previous method of using class name with__init__() method is used.

Code: 11.6. Illustration of super() function in multiple inheritances.

#illustration of super() function in multiple inheritance
class student: #base class 1
‘A student class’
def__init__(self): . #constructor method of base class 1
self.name = (input(‘Enter Student Name:’))
self.rollno = int(input(‘Enter RollNo:’))def display(self): #display method of base class 1
print (“\nName: “, self.name)
print (“\nRollNo:”, self.rollno)class marks: #base class 2
‘A marks class’
def init (self): #Constructor method of base class 2
self.ml = float(input(‘Enter marks of subject 1:’))
self.m2 = float(input(‘Enter marks of subject 2:’))
self.m3 = float(input(‘Enter marks of subject 3:’))
def display_marks(self): #display function of base class 2
print(“\nMarks of subject 1=”, self.ml)
print(“\nMarks of subject 2=”, self.m2)
print(“\nMarks of subject 3=”, self.m3)class result(student, marks): #derived class
‘A result class’
def__init__(self): #Constructor method of derived class
# student.__init__(self)
super().__init__( )
marks.__init__(self)
self.total_marks = self.ml + self.m2 + self.m3
self.perc = self.totalmarks* 100/300
def display result(self): #display_result function of derived class
print(“\nTotal Marks:”, self.total_marks)
print(“\nPercentage:”, self.perc)

r1=result() # object of derived class result
r1.display()
r1.display_marks()
r1.display_result()

Output

Enter Student Name: Rachael
Enter RollNo:111
Enter marks of subject 1:54
Enter marks of subject 2:65
Enter marks of subject 3:73

Name: Rachael

RollNo: 111

Marks of subject 1= 54.0

Marks of subject 2= 65.0

Marks of subject 3= 73.0

Total Marks: 192.0

Percentage: 64

Python Tutorial

Leave a Reply

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