Python Program using multilevel inheritance with 4 levels of hierarchy


In this program, you will learn how to implement multilevel inheritance with 4 levels of hierarchy in Python.


10 + 10 => 20

20 + 10 => 30

Example: How to implement multilevel inheritance with 4 levels of hierarchy in Python

class First:
    def __init__(self):
        self.x = self.y = self.z = 0

    def input1(self):
        self.x = int(input("Enter first number:"))


class Second(First):
    def input2(self):
        self.y = int(input("Enter second number:"))


class Third(Second):
    def findsum(self):
        self.z = self.x + self.y


class Four(Third):
    def result(self):
        print("Sum is:", self.z)


obj = Four()
obj.input1()
obj.input2()
obj.findsum()
obj.result()

Output:

Enter first number:10
Enter second number:20
Sum is: 30