Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMichael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsWhy redefine the add_item() method when it is already inherited from the parent class?
I know I already posted another question about this challenge but something else occurred to me. I copied the code in this challenge into my Python shell, and then created an instance of SortedInventory and ran the add_item() method on that instance and then printed the result and it worked fine. The SortedInventory class already inherits the methods of the Inventory class, so why on earth would you need to define that same method again in the child class and then override it with super()? This just isn't making any sense to me whatsoever.
class Inventory:
def __init__(self):
self.slots = []
def add_item(self, item):
self.slots.append(item)
class SortedInventory(Inventory):
1 Answer
Dave StSomeWhere
19,870 PointsI think you are confusing overriding and super.
Also, please remember that base/parent class is supposed to be somewhat generic and then you add additional attributes and methods in a child/sub class to get more specific.
You can override an attribute or method in the child class just by using the same name and you can declare additional attributes or methods as desired.
To answer your main question:
why on earth would you need to define that same method again in the child class and then override it with super()?
You override the method to add additional functionality (like handling an additional attribute). The function of super() is to execute the parent method. When you execute the override child method, the parent method is not executed (because you overrode it). Chances are that the basic functionality of the parent method should still be executed and instead of re-coding the parent method code in the child, we have super() to execute the parent method.
So, you don't override it with super(), you override it by naming it the same and you execute the parent method with super() to get the best of both worlds (I think of it like an include).
Here's some code to play with that will hopefully help make sense of my above ramble:
class ParentClass:
# parent init
def __init__(self):
self.key_one = 'Parent Attr 1'
self.key_two = 'Parent Attr 2'
# parent method
def method_one(self):
self.key_one = 'method 1, attr 1, parent'
# another parent method
def method_two(self):
self.key_two = 'method 2, attr 2, parent'
class ChildClass(ParentClass):
# override parent init
def __init__(self):
self.key_three = "Child Attr 3"
# call parent init to init attr 1 and 2
super().__init__()
# override parent method 2 by using same name
def method_two(self):
# do parent method stuff
super().method_two()
# do additional child stuff (in addition to parent stuff)
self.key_three = "method 2, attr 3, child"
mama_bear = ParentClass()
print("Mama Bear Class", vars(mama_bear))
mama_bear.method_one()
print("Mama Bear after Method 1", vars(mama_bear))
mama_bear.method_two()
print("Mama Bear after Method 2", vars(mama_bear))
baby_bear = ChildClass()
print("Baby Bear Class", vars(baby_bear))
baby_bear.method_one()
print("Baby Bear after Method 1", vars(baby_bear))
baby_bear.method_two()
print("Baby Bear after Method 2", vars(baby_bear))
# output
Mama Bear Class {'key_one': 'Parent Attr 1', 'key_two': 'Parent Attr 2'}
Mama Bear after Method 1 {'key_one': 'method 1, attr 1, parent', 'key_two': 'Parent Attr 2'}
Mama Bear after Method 2 {'key_one': 'method 1, attr 1, parent', 'key_two': 'method 2, attr 2, parent'}
Baby Bear Class {'key_three': 'Child Attr 3', 'key_one': 'Parent Attr 1', 'key_two': 'Parent Attr 2'}
Baby Bear after Method 1 {'key_three': 'Child Attr 3', 'key_one': 'method 1, attr 1, parent', 'key_two': 'Parent Attr 2'}
Baby Bear after Method 2 {'key_three': 'method 2, attr 3, child', 'key_one': 'method 1, attr 1, parent', 'key_two': 'method 2, attr 2, parent'}
Michael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsMichael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsThanks Dave, your answer really helped to clarify this for me. It isn't really that complicated, but for some reason to me the video made it seem hopelessly complex.