Python Program using hierarchical inheritance


In this program, you will learn how to implement hierarchical inheritance in Python.


20 = 10 + 10

10 = 20 - 10

Example: How to implement hierarchical inheritance in Python

class First:
    def __init__(self):
        self.x = 20
        self.y = 10


class Second(First):
    def findsum(self):
        self.z = self.x + self.y
        print("Sum is:", self.z)


class Third(First):
    def findsub(self):
        self.z = self.x - self.y
        print("Subtraction is:", self.z)


obj1 = Second()
obj1.findsum()

obj2 = Third()
obj2.findsub()

Output:

Sum is: 30
Subtraction is: 10