Python methods overriding

Python methods overriding

Instructor-svgAl-Mamun Sarkar
Mar 25 , 2020

Python methods overriding. The following code shows how to override a method in a subclass using the python programming language.

class Calculator:

    def addition(self, a, b):
        return a + b

    def subtraction(self, a, b):
        return a - b

    def multiplication(self, a, b):
        return a * b

    def division(self, a, b):
        try:
            return a / b
        except ZeroDivisionError:
            return 'It is impossible to divide by zero.'


class SuperCalculator(Calculator):

    def addition(self, a, b, c):
        return a + b + c

    def square(self, a):
        return a * a

    def cube(self, a):
        return a * a * a

my_calculator = SuperCalculator()

temp = my_calculator.addition(23, 47, 12)
print(temp)

temp = my_calculator.subtraction(87, 54)
print(temp)

temp = my_calculator.multiplication(65, 56)
print(temp)

temp = my_calculator.division(852, 76)
print(temp)

temp = my_calculator.square(7)
print(temp)

temp = my_calculator.cube(3)
print(temp)

 

Output:

82
33
3640
11.210526315789474
49
27

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram