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
def display(self): #display method of base class
class rparks(student): #derived class
class result(marks): # new derived class
r1=result() # object of derived class result
Enter Student Name:Richie
Marks of subject 1= 43.0
Marks of subject 2= 55.0
Marks of subject 3= 63.0
Total Marks: 161.0