HomepageTeaching PageResearch PageResources Page

Homework: Candy Bar Inheritance

Let's go even deeper with inheritance and have multiple levels of it. Create a new CandyBar class and include a class attribute with four common candy bar sizes:

class CandyBar(object):
    """Represents a Candy Bar."""
    size_names = ["Bite Size", "Fun Size", "Full Size", "King Size"]
        

Now add some useful methods for this class. Notice that I'm not including an __init__ method.

    def get_size_string(self):
        """Returns a string describing the size of this bar."""
        return CandyBar.size_names[self.length]
        
    def __str__(self):
        """Returns a string representation of this."""
        string = "A delicious " + self.get_size_string() + " candy bar with " + strings_list_to_one_string(self.get_ingredients())
        return string
        

You'll notice that I'm using the strings_list_to_one_string function that we defined in projects earlier. Next let's add a subclass of CandyBar, PayDay:

    
class PayDay(CandyBar):
    """Represents a Pay Day bar."""
    
    def __init__(self, length):
        """Constructor."""
        self.length = length
        
    def get_ingredients(self):
        """Returns the ingredients of this as a list of strings."""
        return ["peanuts", "caramel"]
        

A class can have multiple subclasses; let's add another one:

class ThreeMusketeers(CandyBar):
    """Represents a 3-Musketeers bar."""
    
    def __init__(self, length):
        """Constructor."""
        self.length = length
        
    def get_ingredients(self):
        """Returns the ingredients of this as a list of strings."""
        return ["chocolate", "nougat"]
        

A subclass can also be inherited from! Here's another subclass to add:

     
class MilkyWay(ThreeMusketeers):
    """Represents a Milky Way bar."""
    
    def __init__(self, length):
        """Constructor"""
        ThreeMusketeers.__init__(self, length)
        
    def get_ingredients(self):
        """Returns the ingredients of this as a list of strings."""
        ingredients = super(MilkyWay, self).get_ingredients()
        ingredients.append("caramel")
        return ingredients
        

Now ThreeMusketeers is both a subclass (of CandyBar) and a superclass (of MilkyWay). That means MilkyWay inherits everything from ThreeMusketeers, including the things inherited from CandyBar!

Now let's take a closer look at the MilkyWay.get_ingredients method.

>>> bar = MilkyWay(2)
>>> bar.get_ingredients()
['chocolate', 'nougat', 'caramel']
        

What's happening here?

Can we do another level of inheritance? Yes! What candy bar has all the ingredients of a Milky Way, plus an extra ingredient?

class Snickers(MilkyWay):
    """Represents a Snickers bar."""
    
    def __init__(self, length):
        """Constructor"""
        super(Snickers, self).__init__(length)
        
    def get_ingredients(self):
        """Returns the ingredients of this as a list of strings."""
        ingredients = super(Snickers, self).get_ingredients()
        ingredients.append("peanuts")
        return ingredients
        

Now make a guess about what will happen if you call Snickers.get_ingredients. Then try it out:

>>> bar = Snickers(1)
>>> bar.get_ingredients()
???