Exercise: Overriding Inherited Method
Use what you’ve learned so far to override a method that is inherited from a parent class.
We'll cover the following
Problem statement
You have been provided with a Person
class with the variables name
and age
and a greet
method. The greet
method of the Person
class is simply printing Hello for now.
You have also been provided a Customer
class that inherits the name
and age
variables and the greet
method from the Person
class.
Therefore, your task is to write the greet()
function of the Customer
class that overrides the inherited greet
method of the Person
class. Instead of simply printing Hello, it will now print the values of the name
and age
variables as well as the value of the no_of_purchases
variable, which will be the additional information in our Customer
class.
Input
class Person(object):
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello")
class Customer(Person):
pass # Replace with your code
Output
Note: In the following example, the
Customer
class hasn’t been fully filled. The small preview given below is just to give you an idea of what you need to do.
class Customer(Person):
def greet(self):
Challenge
This problem has been designed for you to practice freely, so try to solve it on your own first. Take some time and think about the different concepts we’ve been explored in the course so far.
If you feel stuck, you can always check out the solution review provided in the next lesson.
Good luck!
Get hands-on with 1400+ tech skills courses.