*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 multiple inheritance. 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
""" Multiple inheritance can be thought of in terms of inherting abilities from two parents, or perhaps a grandparent as well! If Mum can think, your grandfather could play chess, and your father could play the piano, potentially you're a wildly talented individual that inherited all of these attributes from multiple parents! In programming a derived class can have more than one base/parent class having access to all the relevant attributes and methods Task: Write an object oriented program that performs the following tasks: 1. Create a class called Wardrobe from the base class Furniture 2. Oak should be the type of furniture that is used by all furnitures by default 3. The user can be given an option to change the type of wood used for the Wardrobe if he/she wishes to 4. The height of the wardrobe should be a property that should not be altered outside the class """ class Person: #defining constructor -all people in the world typically will have attributes like name and age def __init__(self, personName, personAge): self.name = personName self.age = personAge #defining class methods def showName(self): print(self.name) def showAge(self): print(self.age) #end of class definition # defining another class class Student: # The Student class defines attributes such as ID no. and main subject being studied def __init__(self, studentId,subject): self.studentId = studentId self.subject=subject def getId(self): return self.studentId def getSubject(self): return self.subject class Enrolled(Person, Student): # extends both Person and Student class #Note the order in which you call the parent classes matters! #If you have an attribute x in the Person AND Student classes, a call to print it, would print the attribute belonging to the first class called def __init__(self, name, age, studentId,subject,enrolled_date): Person.__init__(self, name, age) Student.__init__(self, studentId,subject) self.enrolled_date=enrolled_date def getDate(self): return self.enrolled_date #======DEMONSTRATION OF MULTIPLE INHERITANCE ========== if __name__ == "__main__": #see note below for an explanation of why this is used # Create an object of the subclass enrolled1 = Enrolled('Joe Bloggs', 33, '001',"COMPUTING 101","19/01/2019") enrolled1.showName() #this attribute is inherited from the super class Person print("ID No:",enrolled1.getId()) #this is inherited from Student print("Date enrolled:",enrolled1.getDate()) #this is calling a method from the Enrolled Class print("Subject:",enrolled1.getSubject()) #this method is inherited from Student """ NOTE: The global variable, __name__, in the module that is the entry point to your program, is '__main__'. So, code in this if block will only run if that module is the entry point to your program. Usually useful if you're writing a Python script designed to be used as a module as you know it will execute correctly. Good practice to use this when programming """
Download Python 3 here: