~ OOP,Classes,Python,Understanding multi level inheritance in Python


A look at multilevel inheritance in Python

*Please Note* - all presentations and code challenges+solutions are available to subscribing members. It's now time to look even deeper at this beautiful concept of inheritance and explore multi level inheritance. It will be important for you to understand the difference between the kinds of inheritance in order to be able to apply your knowledge and code solutions. You should also be becoming quite familiar with how classes work in practice, and how you can use them in your projects. Do watch the video below which explains the concept, goes through the coding example and then attempt the challenges which are presented inside the code yourself

Video

Code and Challenge #1

"""
MULTILEVEL INHERITANCE
Real world example: If your father played chess, you inherited this ability, but you also like to dance. Your
son, if you have one, could inherit both your father's ability to play chess as well as your dance moves!

While multiple inheritance is when a child class derives attributes and methods from more than one
parent class, multilevel inheritance is referring to a series of inheritance, with a class at the first level
and a class at the second level. Creation of a class at a third level would mean that this class would
inherit all the attributes and methods of the class at the second level (and in so doing, access to the
attributes and methods of the class at the first level, as the second level class inherits from it!)

Definition:  Mechanism by which a derived class inherits from a base class which has been derived from
another base class

TASK:

To test out the functionality of multilevel inheritance, do the following and test it:
1. Create a method in the TopComputingStudent Class called "Eligible_for_Reward_Trip". This method checks to
see if the student is above the age of 13 AND if their total marks is above 90. If elibility metL: print: You are eligible for the reward trip to London!
2. Create an object called s1, and call the "Eligible_for_Reward_Trip" method to see if it works. Note that this method would call
on attributes defined in parent classes.

"""

class Student: #Define a Student Class
    def __init__(self):
        self.name = input("Name: ")
        self.age = int(input("Age: "))
        self.gender = input("Gender: ")
    def printStudent(self):
        print("=====Printing Student Details========")
        print(self.name)
        print(self.age)
        print(self.gender)

class ComputingStudent(Student): #the ComputintStudent Class inherits from the Student Class
    def __init__(self):
        super(ComputingStudent, self).__init__()
        print("Enter the marks for your first three tests:")
        self.test1= int(input("Test1: "))
        self.test2 = int(input("Test2: "))
        self.test3 = int(input("Test3: "))

    def getMarks(self):
        print("====PRINTING YOUR TEST SCORES==")
        print(self.test1)
        print(self.test2)
        print(self.test3)
        

class TopComputingStudent(ComputingStudent):#The TopComputingStudent Class inherits from the Computing Student which in turn inherits from Student!
    def __init__(self):
        super(TopComputingStudent, self).__init__()
        self.total_marks = self.test1 + self.test2 + self.test3

    def display(self):
        print("\n\nName: ",self.name)
        print("Age: ",self.age)
        print("Gender: ",self.gender)

        print("Total Marks: ", self.test1 + self.test2 + self.test3)

    def eligible_for_reward(self):
        pass
    #YOUR CODE GOES HERE 

#======DEMONSTRATION OF MULTIPLE INHERITANCE ==========
if __name__ == "__main__":

    s2=TopComputingStudent()
    s2.printStudent()
    s2.getMarks()



    


"""
Useful notes:
#1 inheritance tree is (from a design POV) important to consider logically
Inheritance, is semantically, a "is a" relationship (if B inherit from A, then B is a A).
In this case, a top computing student is a computing student and a computing student is a Student!

#2     def __init__(self):
        super(TopComputingStudent, self).__init__()

        super() calls the __init__ method of the parent class. So the above example passes the name
        of the current class which in turn will call the __init__ method of the parent class.
"""



Download Python 3 here: