Example of Multilevel Inheritance in Python (2)

Here, we are going to learn about the multilevel inheritance in Python with an example.
Submitted by Shivang Yadav, on February 19, 2021

Python Multilevel Inheritance:

Multilevel inheritance in programming (general in object-oriented program). When a base class is derived by a derived class which is derived by another class, then it is called multilevel inheritance.

The below diagram will make things clearer,

All Classes & methods used in the program:  

  • Class: Student
    • Method: getStudentInfo() : gets student's roll number and name from user.
    • Method: printStudentInfo() : prints student's roll number and name.
  • Class: Bsc
    • Method: getBsc() : gets student's marks from user.
    • Method: putPerks() : prints student's marks.
    • Method: calcTotalMarks() : returns the sum of marks.
  • Class : Result
    • Method: getResult() : gets student's information and calculate total marks.
    • Method: printResult() : prints prints student information.

Program to illustrate multilevel inheritance in Python

class Student:
    def getStudentInfo(self):
        self.__rollno=input("Enter Roll Number: ")
        self.__name=input("Enter Name: ")

    def printStudentInfo(self):
        print("Roll Number : ", self.__rollno, "Name : ", self.__name)

class Bsc(Student):
    def getBsc(self):
        self.getStudentInfo()
        self.__p = int(input("Enter Physics Marks: "))
        self.__c = int(input("Enter Chem Marks: "))
        self.__m = int(input("Enter Maths Marks: "))

    def printBsc(self):
         self.printStudentInfo()
         print("Marks in different Subjects : ", self.__p,self.__c,self.__m)

    def calcTotalMarks (self):
        return(self.__p+self.__m+self.__c)

class Result(Bsc):
    def getResult(self):
        self.getBsc()
        self.__total=self.calcTotalMarks()

    def putResult(self):
        self.printBsc()
        print("Total Marks out of 300 : ", self.__total)

student = Result()
student.getResult()
student.putResult()

Output:

Enter Roll Number: 101
Enter Name: Shivang Yadav
Enter Physics Marks: 65
Enter Chem Marks: 78
Enter Maths Marks: 80
Roll Number :  101 Name :  Shivang Yadav
Marks in different Subjects :  65 78 80
Total Marks out of 300 :  223

Python class & object programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.