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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.