Recall & Review
beginner
What does it mean to extend parent behavior in Python classes?
It means adding new features or modifying existing ones in a child class while still using the behavior defined in the parent class.Click to reveal answer
beginner
How do you call a method from the parent class inside a child class in Python?You use <code>super().method_name()</code> to call the parent class method from the child class.Click to reveal answer
beginner
Why would you want to extend parent behavior instead of completely replacing it?
To keep the original functionality and add extra features or changes without losing what the parent class already does.Click to reveal answer
beginner
Example: What will this code print?
<pre>class Parent:
def greet(self):
print('Hello from Parent')
class Child(Parent):
def greet(self):
super().greet()
print('Hello from Child')
c = Child()
c.greet()</pre>It will print:
Hello from Parent
Hello from Child
Because the child calls the parent's greet method first, then adds its own message.
Click to reveal answer
beginner
What happens if you forget to call
super() when overriding a method?The parent class method won't run, so you lose the original behavior and only the child's code runs.Click to reveal answer
What keyword is used to call a parent class method in Python?
✗ Incorrect
The
super() function is used to access methods from the parent class.If a child class overrides a method but does NOT call
super(), what happens?✗ Incorrect
Without
super(), only the child method runs, skipping the parent method.Why use
super() inside an overridden method?✗ Incorrect
super() calls the parent method so you can add to it instead of replacing it.What is the output of this code?
class A:
def say(self):
print('A')
class B(A):
def say(self):
print('B')
super().say()
b = B()
b.say()✗ Incorrect
The child prints 'B' first, then calls the parent method which prints 'A'.
Extending parent behavior helps to:
✗ Incorrect
Extending behavior allows reuse of existing code while adding new functionality.
Explain how to extend a parent class method in Python and why it is useful.
Think about how to keep old behavior and add new steps.
You got /4 concepts.
Describe what happens if you override a method without calling super() in a child class.
Consider what part of the code is skipped.
You got /3 concepts.