In addition to defining new methods in the child class, you can also override methods and variables of your parent class.
How to Override a Method
In the code snippet below, you'll override the .expire() method in your Spice() class:
class Spice(Ingredient):
"""Models a spice to flavor your food."""
def expire(self):
print(f"your {self.name} has expired. it's probably still good.")
self.name = "old " + self.name
In this code example, you wrote a new .expire() method that is unique to your Spice() child class. Because your child class already had a .expire() method that it inherited from its parent class, defining another method with the same name will override the inherited .expire() method.
Multiple Method Versions
Objects of both classes now still have access to a method called .expire(), but calling the method will have different effects depending on what object you call it on:
c = Ingredient("carrots", 3)
p = Spice("pepper", 20)
c.expire() # OUTPUT: whoops, these carrots went bad...
p.expire() # OUTPUT: your pepper has expired. it's probably still good.
If you print out the objects themselves to see their string representations, which you defined as f"{self.name} ({self.amount})" in the __str__() method of your parent class, you'll see that the .name attribute of the different classes has changed in different ways:
print(c) # OUTPUT: expired carrots
print(p) # OUTPUT: old pepper
While you added the string expired to the Ingredient() object, c, the Spice() object, p, has a different string, old in front of the object's initial .name attribute.
This type of method overriding is an example of polymorphism in Python. Both classes have a method with the same name, but the methods have different effects.
Tasks
- Edit the .expire() method of your Spice class to include a conditional statement. If the name of your Spice() instance is "salt", then print a message that clarifies that salt never expires.
- For example, calling
s.expire()on theSpice()objects = Spice('salt', 200)should return a message like:salt never expires! ask the sea!. - What happens when you make an
Ingredient()with the name"salt"and call.expire()on it?
There's a lot to take in here, so you'll practice creating child classes and overriding methods some more.
More Practice
Your next task will be to create another child class called Vegetable() that inherits from Ingredient() as well. Pull out your notebook and your pencil, and think about the following questions before you start writing your code:
- Which aspects of the parent class will you take over?
- What parts do you want to override?
- Is there something you'd like to add to the child class?
Once you have a plan for your new Vegetable() child class, go ahead and build it out.
Next, you'll override the __init__() method of your parent class, which will have effects on how you need to instantiate objects of your child class.
Summary: Python Subclass Method Override
- Child classes can override parent methods
- Overriding parent methods is done by re-defining a new method or attribute with the same name inside the child class
- The redefinition of the parent method automatically overrides the method
- Method overriding offers additional customization while maintaining a consistent interface for the development team