Q:

Example of Multilevel Inheritance in Python (2)

belongs to collection: Python class & object programs

0

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
    • MethodgetStudentInfo() : gets student's roll number and name from user.
    • MethodprintStudentInfo() : prints student's roll number and name.
  • Class: Bsc
    • MethodgetBsc() : gets student's marks from user.
    • MethodputPerks() : prints student's marks.
    • MethodcalcTotalMarks() : returns the sum of marks.
  • Class : Result
    • MethodgetResult() : gets student's information and calculate total marks.
    • MethodprintResult() : prints prints student information.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python class & object programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Example of hierarchical inheritance in Python... >>
<< Example of multilevel inheritance in Python...