Solution Review: Overriding Inherited Method
This lesson explains the solution for the "overriding methods" exercise.
We'll cover the following...
Solution
Press + to interact
class Person(object):def __init__(self, name, age):self.name = nameself.age = agedef greet(self):print("Hello")class Customer(Person):def __init__(self, name, age, no_of_purchases):super().__init__(name, age)self.no_of_purchases = no_of_purchasesdef greet(self):print(self.name + ",", self.age, "years old, purchased", self.no_of_purchases, "items")# name = Josh, age = 25, no_of_purchases = 10c = Customer("Josh", 25, 10)c.greet()
Explanation
Since we need to print the additional no_of_purchases
variable in our greet
function, we need to write a new __init__
constructor at line 10, overriding the ...