'''lecture_29_csstudent.py Show inheritance with the CSStudent (child class) that inherits from ColbyStudent (parent class) TODO: 1. is-a relationship 2. CSStudent constructor signature: def __init__(self, name, year, favLang) - Show with redundant code. - Code reuse: call parent constructor (with super() and directly) 3. When instance variables in parent inherited by child - Show __init__ with/without calling parent constructor 4. set/get methods for CSStudent-specific vars - Show access with ColbyStudent and CSStudent objects 5. Write printGrades in parent (100 point scale) - Show OVERRIDE in child (30 point scale) - Show EXTENDING in (both scales) 6. Symbol table for `newCSStudent.printGrades()` and `newColbyStudent.printGrades()` Oliver W. Layton CS 151: Computational Thinking: Visual Media Fall 2020 ''' class ColbyStudent: lastDayOfClass = 'Dec 4' def __init__(self, name, year=2022): # Initialization data self.name = name self.year = year # Derived data self.grades = [] self.gpa = 0.0 def getName(self): return self.name def getGrades(self): # return self.grades # shallow copy / nickname return self.grades[:] # deep copy by independence rule for slicing lists def addGrade(self, newGrade): self.grades.append(newGrade) def getLastDay(self): return ColbyStudent.lastDayOfClass class CSStudent(ColbyStudent): def __init__(self, name, year, favLang): # Leveraging/running code in parent class # Parent class has another name: super class # Child is called subclass super().__init__(name, year) # self.name and self.year get defined # Another way to access parent version of a method: # ColbyStudent.__init__(self, name, year) self.favLang = favLang # If super class init not called, self.gpa never defined so following # line of code will crash print('GPA is', self.gpa) print('Accessing getFavLang method:', self.getFavLang()) # Duplicated code from parent class # self.name = name # self.year = year # self.favLang = favLang def getFavLang(self): return self.favLang if __name__ == '__main__': colbyStu = ColbyStudent('Jane', 2022) csStu = CSStudent('Jim', 2024, 'Python') print('CS Student fav language:', csStu.getFavLang()) print('CS Student name', csStu.getName())