Python Inheritance

Author: Al-mamun Sarkar Date: 2020-03-25 18:47:56

Python Inheritance. The following code shows how to inherit a class in a 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 square(self, a):
        return a * a

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


my_calculator = SuperCalculator()

temp = my_calculator.addition(23, 47)
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:

70
33
3640
11.210526315789474
49
27