Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is method overriding in Python?
Method overriding happens when a child class provides its own version of a method that already exists in its parent class. This new method replaces the parent's method when called on the child class object.
Click to reveal answer
intermediate
How does Python decide which method to call when a method is overridden?
Python uses the method of the child class if it exists. If the child class does not have the method, Python looks up the parent class method. This is called dynamic method dispatch.
Click to reveal answer
intermediate
Can a child class call the parent class's overridden method? How?
Yes, the child class can call the parent class's method using the super() function. For example, super().method_name() calls the parent method inside the child method.
Click to reveal answer
beginner
What happens if the child class does not override a method from the parent class?
If the child class does not override the method, the method from the parent class is used when called on the child class object.
Click to reveal answer
beginner
Why is method overriding useful in programming?
It allows child classes to customize or extend the behavior of parent classes without changing the parent code. This helps create flexible and reusable code.
Click to reveal answer
What does method overriding allow a child class to do?
AProvide a new version of a method from the parent class
BDelete a method from the parent class
CCreate a method with a different name
DPrevent the parent class from being used
✗ Incorrect
Method overriding lets the child class provide its own version of a method that exists in the parent class.
How can a child class call the original method from its parent class after overriding it?
AIt is not possible
BBy renaming the method
CBy deleting the child method
DUsing the super() function
✗ Incorrect
The super() function allows the child class to access the parent class's method even after overriding it.
If a method is not overridden in the child class, which method is called?
AAn error occurs
BA random method
CThe parent class method
DThe child class method
✗ Incorrect
If the child class does not override the method, the parent class's method is used.
What is the term for Python choosing which method to call at runtime?
AStatic typing
BDynamic method dispatch
CMethod hiding
DCompile-time binding
✗ Incorrect
Dynamic method dispatch means Python decides at runtime which method to call, especially with overridden methods.
Why is method overriding important in object-oriented programming?
AIt allows customizing inherited behavior
BIt prevents inheritance
CIt deletes parent classes
DIt creates new classes automatically
✗ Incorrect
Method overriding lets child classes change or extend the behavior they inherit from parent classes.
Explain method overriding and how Python decides which method to run when a method is overridden.
Think about how child and parent classes interact when they have methods with the same name.
You got /3 concepts.
Describe how to call a parent class's method from a child class when the method is overridden.
Remember the special function that helps access parent methods.
You got /3 concepts.
Practice
(1/5)
1. What does method overriding allow a child class to do in Python?
easy
A. Prevent the parent class method from being used anywhere
B. Create a new method with a different name
C. Change the behavior of a method inherited from the parent class
D. Automatically call the parent class method without code
Solution
Step 1: Understand method overriding concept
Method overriding means the child class provides its own version of a method that exists in the parent class.
Step 2: Identify what overriding changes
The child class method replaces the parent's method behavior when called on the child instance.
Final Answer:
Change the behavior of a method inherited from the parent class -> Option C
Assuming parent method is called automatically without super()
2. Which of the following is the correct way to override a method named greet in a child class?
easy
A. def greet(self, extra):\n print('Hello from child')
B. def greet(self):\n print('Hello from child')
C. def greet_child(self):\n print('Hello from child')
D. def greet():\n print('Hello from child')
Solution
Step 1: Match method name exactly
Overriding requires the child method to have the same name as the parent method, here 'greet'.
Step 2: Check method signature
The method must include 'self' as the first parameter to be a proper instance method.
Final Answer:
def greet(self):\n print('Hello from child') -> Option B
Quick Check:
Same name and self parameter = correct override [OK]
Hint: Override by matching method name and self parameter [OK]
Common Mistakes:
Changing method name instead of overriding
Omitting self parameter in method definition
Adding extra parameters that don't match parent method
3. What will be the output of this code?
class Parent:
def greet(self):
print('Hello from Parent')
class Child(Parent):
def greet(self):
print('Hello from Child')
obj = Child()
obj.greet()
medium
A. Hello from Parent
B. Error: greet method not found
C. Hello from Parent\nHello from Child
D. Hello from Child
Solution
Step 1: Identify method overriding
The Child class defines its own greet method, overriding Parent's greet.
Step 2: Determine which method is called
Calling obj.greet() on a Child instance calls the Child's greet method, printing 'Hello from Child'.
Final Answer:
Hello from Child -> Option D
Quick Check:
Child method overrides Parent method = 'Hello from Child' [OK]
Hint: Child method runs when overridden, not parent's [OK]
Common Mistakes:
Expecting both parent and child messages to print
Thinking parent method runs instead of child
Assuming error due to method name conflict
4. Find the error in this code that tries to override a method:
class Parent:
def show(self):
print('Parent show')
class Child(Parent):
def show():
print('Child show')
obj = Child()
obj.show()
medium
A. Missing self parameter in Child's show method
B. Parent class method show is private
C. Child class should not override show method
D. obj.show() should be called as Child.show(obj)
Solution
Step 1: Check method signature in Child class
The Child's show method is missing the 'self' parameter, so it is not a proper instance method.
Step 2: Understand impact of missing self
Calling obj.show() will cause a TypeError because Python expects the first argument (self) but none is defined.
Final Answer:
Missing self parameter in Child's show method -> Option A
Quick Check:
Instance methods must have self parameter [OK]
Hint: Instance methods always need self as first parameter [OK]
Common Mistakes:
Ignoring missing self parameter
Thinking method overriding is not allowed
Believing calling method differently fixes error
5. Given this code, what will be the output?
class Parent:
def greet(self):
print('Hello from Parent')
class Child(Parent):
def greet(self):
super().greet()
print('Hello from Child')
obj = Child()
obj.greet()
hard
A. Hello from Parent\nHello from Child
B. Hello from Child
C. Hello from Parent
D. Error: super() used incorrectly
Solution
Step 1: Understand super() call in Child's greet
The Child's greet method calls super().greet(), which runs the Parent's greet method first.
Step 2: Follow the print statements
First, 'Hello from Parent' is printed, then 'Hello from Child' is printed after.
Final Answer:
Hello from Parent\nHello from Child -> Option A
Quick Check:
super() calls parent method before child code [OK]
Hint: super() runs parent method before child code [OK]