Python Programming – Python Multilevel Inheritance

In the concept of multilevel inheritance, we see that a derived class can be further inherited by a new derived class. In other words, the features of the base class and a derived class can be inherited by a new derived class. The programming illustration of multilevel inheritance is given in Code 11.3. It is almost similar to the program code given for multiple inheritances, where there were two base classes and one derived class. Herein, with multilevel inheritance, we create a base class student with similar data members and functions as mentioned in the previous example.

Then, we derive marks class from the student class that means the marks class inherits the traits of the student class. Further, we create a new derived class result, which is inherited from the marks class. Eventually, we have three classes with two levels student->marks->result. Thus, the result class contains the traits of both the class’s student as well as marks. Alike, previous program the object of the result class is created, by which all the operations are performed like earlier, and the result is displayed.

Code: 11.3. Illustration of multilevel inheritance,

#illustration of multilevel inheritance

class student: #base class
‘A student class’
def init (self):                                      #Constructor method of base class = (input(‘Enter Student Name:’))
self.rollno = int(input(‘Enter RollNo:’))

def display(self):                                     #display method of base class
print (“\nName:”,
print (“\nRollNo:”, self.rollno)

class rparks(student):                                #derived class
‘A marks class’
def __init__(self):                               #Constructor method of derived class = float(input(‘Enter marks of subject 1:’))
self.m2 = float(input(‘Enter marks of subject 2:’))
self.m3 = float(input(‘Enter marks of subject 3:’))
def display_marks(self):                         #display function of base class 2
print(“\nMarks of subject 1=”,
print(“\nMarks of subject 2=”, self.m2)
print(“\nMarks of subject 3=”, self.m3)

class result(marks):                         # new derived class
‘A result class’
def__init__(self):                                  #Constructor method of new derived class
marks, __init__(self)
self,total_marks = + self.m2 + self.m3
self.perc = self.total_marks*100/300
def display_result(self):                          #display_result function of derived class
print(“\nTotal Marks:”, self.total_marks)
print(“\nPercentage:”, self.perc)

r1=result() # object of derived class result
r1 .display_marks()
r1 .display_result()


Enter Student Name:Richie
Enter RollNo: 114
Enter marks of subject 1:43
Enter marks of subject 2:55
Enter marks of subject 3:63

Name: Richie

RollNo: 114

Marks of subject 1= 43.0

Marks of subject 2= 55.0

Marks of subject 3= 63.0

Total Marks: 161.0

Percentage: 53.666666666666664

Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *