'''lecture30_veggie_extend.py `SweetPotato` inherits from `Veggie`. - The `SweetPotato` inherits getName method. - The `SweetPotato` has isOrange method, `Veggie` does not. Oliver W. Layton CS 151: Computational Thinking: Visual Media Spring 2021 ''' class Veggie: def __init__(self, name): self.name = name def getName(self): return self.name # As independent, separate class with lots of redundancy, which is not good # class SweetPotato: # def __init__(self, name): # self.name = name # def getName(self): # return self.name class SweetPotato(Veggie): # SweetPotato is the class name. Veggie is the parent class # Inheritance: as if __init__ and getName of Veggie copy-pasted here # isOrange is ONLY defined in the CHILD, the parent does NOT have access def isOrange(self): return True if __name__ == '__main__': pot = Veggie('Potato') sweetPot = SweetPotato('Sweet potato') print('Veggie name:', pot.getName()) print('Sweet potato name:', sweetPot.getName()) print('Is this an orange sweet potato?', sweetPot.isOrange()) # print('Is this an orange potato?', pot.isOrange())