Python Object-Oriented Python Inheritance Super!

Michael Cook
Michael Cook
18,215 Points

Why 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.

inventory.py
class Inventory:
    def __init__(self):
        self.slots = []
    def add_item(self, item):
        self.slots.append(item)

class SortedInventory(Inventory):

1 Answer

Dave StSomeWhere
Dave StSomeWhere
19,786 Points

I 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
Michael Cook
18,215 Points

Thanks 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.