Monday, September 5, 2022

How to perform method overriding?

To implement the method overriding procedure you have to understand basic class inheritance. Overriding is basically altering or replacing the method defined in parent class with a new method defined in child class. Let's say that you have created the Parent class and the Child class. In the Parent class you have created the __init__ method that will initialize two attributes x and y every time object is instantiated from that class. In child class you have also created __init__ method that will initialize three attributes x, y, and z every time the object is instantiated from the child class. When the object is instantiated from child class it will rewritten __init__ method of the parent class. In the following example we will created two classes ParentCalculator with four basic mathematical operations (addition, subtraction, multiplication and division) with only two attributes x and y, and ChildCalculator that will perform all these mathematical operations but with three variables.
Example 1
class ParentCalculator:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self):
return self.x + self.y
def sub(self):
return self.x - self.y
def mul(self):
return self.x*self.y
def div(self):
return self.x/self.y
class ChildCalculator(ParentCalculator):
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def add(self):
return self.x + self.y + self.z
def sub(self):
return self.x - self.y - self.z
def mul(self):
return self.x*self.y*self.z
def div(self):
return self.x/self.y/self.z
#Testing the ParentCalculator
test = ParentCalculator(5,10)
print("{} + {} = {}".format(test.x, test.y, test.add()))
print("{} - {} = {}".format(test.x, test.y, test.sub()))
print("{}*{} = {}".format(test.x, test.y, test.mul()))
print("{}/{} = {}".format(test.x, test.y, test.div()))
#Testing the ChildCalculator
test2 = ChildCalculator(5,10,15)
print("{} + {} + {} = {}".format(test2.x, test2.y, test2.z, test2.add()))
print("{} - {} - {} = {}".format(test2.x, test2.y, test2.z, test2.sub()))
print("{}*{}*{} = {}".format(test2.x, test2.y, test2.z, test2.mul()))
print("{}/{}/{} = {}".format(test2.x, test2.y, test2.z, test2.div()))
The generated output is shown below.
5 + 10 = 15
5 - 10 = -5
5*10 = 50
5/10 = 0.5
5 + 10 + 15 = 30
5 - 10 - 15 = -20
5*10*15 = 750
5/10/15 = 0.03333333333333333
The problem can arise when we try to use ParentClass with three variables.
            test3 = ParentClaculator(5,10,15)
        
This will rise the TypeError.
TypeError: __init__() takes 3 positional arguments but 4 were given
When the object is initialized using Parent Calculator the __init__ method takes three arguments self, x, and y. However, in this case we provided 3 attributes thinking it would define values for x, y and z attributes like in child class. The error was generated since there is an extra argument which is not defined in the Parent Calculator.
There is yet another problem in this example. The ParentCalculator and the ChildCalculator have duplicate code to set up the x and y attributes. This can make code maintenance complicated. What we want is to execute the original __init__ method in the ParentCalculator class. This can be solved using super() function. This function returns the object as an instance of the parent class (ParentCalculator) which allows us to call the parent method directly. So in Example 1 we will implement the super function to the ChildCalculator.
Example 2Create ParentCalculator with four basic operations and Child Calculator with extensions made to methods of ParentCalculator using super method.
class ParentCalculator:
def __init__(self, x, y):
self.x = x
self.y = y
def add(self):
return self.x + self.y
def sub(self):
return self.x - self.y
def mul(self):
return self.x*self.y
def div(self):
return self.x/self.y
class ChildCalculator(ParentCalculator):
def __init__(self, x, y, z):
super().__init__(x, y)
self.z = z
def add(self):
super().add()
return self.x + self.y + self.z
def sub(self):
super().sub()
return self.x - self.y - self.z
def mul(self):
super().mul()
return self.x*self.y*self.z
def div(self):
super().div()
return self.x/self.y/self.z
test = ChildCalculator(5,10,15)
print(test.add())
print(test.sub())
print(test.mul())
print(test.div())
The output of the previous code is given below.
30
-20
750
0.03333333333333333

No comments:

Post a Comment