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.
- Python Programming – Python Multiple Inheritance
- Python Programming – Inheritance
- Python Programming – Python Single Inheritance
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 |
Output
Enter Student Name:Richie 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 |