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 occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The subclass's method replaces the superclass's method when called on an instance of the subclass.
Click to reveal answer
beginner
Why do we use method overriding?
We use method overriding to change or extend the behavior of a method inherited from a parent class, allowing subclasses to customize or improve functionality without changing the parent class.
Click to reveal answer
beginner
Example of method overriding in Python:<br><pre>class Animal:
def sound(self):
return "Some sound"
class Dog(Animal):
def sound(self):
return "Bark"</pre><br>What will <code>Dog().sound()</code> return?
It will return "Bark" because the Dog class overrides the sound method of the Animal class with its own version.
Click to reveal answer
intermediate
Can method overriding be used to call the parent class method as well?
Yes. Inside the overriding method, you can call the parent class method using <code>super().method_name()</code> to reuse or extend the original behavior.
Click to reveal answer
beginner
What happens if a subclass does not override a method from its superclass?
If a subclass does not override a method, it inherits the method from the superclass and uses that implementation when the method is called.
Click to reveal answer
What does method overriding allow a subclass to do?
ACreate a new method with a different name
BPrevent the superclass method from being called
CProvide a new version of a method already defined in the superclass
DChange the superclass's source code
✗ Incorrect
Method overriding lets a subclass provide its own version of a method that exists in the superclass.
How do you call the parent class method inside an overriding method?
AUsing <code>override.method_name()</code>
BUsing <code>self.method_name()</code>
CUsing <code>parent.method_name()</code>
DUsing <code>super().method_name()</code>
✗ Incorrect
The super() function is used to call the parent class method inside an overriding method.
If a subclass does not override a method, what happens when the method is called on the subclass instance?
AThe method from the superclass is used
BAn error occurs
CThe method is ignored
DThe program crashes
✗ Incorrect
The subclass inherits the method from the superclass and uses it if it does not override it.
Which of the following is NOT true about method overriding?
AIt requires changing the superclass code
BIt supports polymorphism
CIt allows customizing inherited methods
DIt happens in subclass methods
✗ Incorrect
Method overriding does NOT require changing the superclass code; it happens by defining a method with the same name in the subclass.
What will this code print?
class A:
def greet(self):
return 'Hello from A'
class B(A):
def greet(self):
return 'Hello from B'
print(B().greet())
AHello from A
BHello from B
CError
DHello from A and B
✗ Incorrect
Class B overrides the greet method, so calling greet on B's instance returns 'Hello from B'.
Explain method overriding and why it is useful in object-oriented programming.
Think about how a child class can change a parent's method.
You got /4 concepts.
Describe how to call a parent class method from an overriding method in Python.
Use a built-in function to access the parent method.
You got /3 concepts.
Practice
(1/5)
1.
What is method overriding in Python?
Choose the best description.
easy
A. A child class provides a new version of a method from its parent class.
B. A method that is called automatically when an object is created.
C. A method that cannot be changed once defined in a class.
D. A method that is only accessible inside the class it is defined.
Solution
Step 1: Understand method overriding concept
Method overriding means a child class changes a method from its parent by defining a method with the same name.
Step 2: Match description to concept
A child class provides a new version of a method from its parent class. correctly describes this behavior, while others describe different concepts.
Final Answer:
A child class provides a new version of a method from its parent class. -> Option A
Hint: Child class method with same name replaces parent method [OK]
Common Mistakes:
Confusing overriding with overloading
Thinking overriding means creating a new method
Believing methods cannot be changed in child classes
2.
Which of the following is the correct way to override a method greet in a child class?
class Parent:
def greet(self):
print("Hello from Parent")
class Child(Parent):
# What goes here?
easy
A. def greet():
print("Hello from Child")
B. def greet(self):
print("Hello from Child")
C. def greet(self, name):
print(f"Hello {name} from Child")
D. def greet(self):
return "Hello from Child"
Solution
Step 1: Check method signature for overriding
The child method must have the same name and parameters as the parent method to override it properly.
Step 2: Compare options
def greet(self):
print("Hello from Child") matches the parent's method signature exactly. def greet():
print("Hello from Child") misses 'self', C adds a parameter, and D returns a string instead of printing.
Final Answer:
def greet(self):\n print("Hello from Child") -> Option B
Quick Check:
Same method name and parameters = correct override [OK]
Hint: Match method name and parameters exactly to override [OK]
Common Mistakes:
Omitting 'self' parameter in method
Changing method parameters when overriding
Returning value instead of matching parent's behavior
3.
What will be the output of the following code?
class Animal:
def sound(self):
print("Some sound")
class Dog(Animal):
def sound(self):
print("Bark")
pet = Dog()
pet.sound()
medium
A. Some sound
B. None
C. Error: Method not found
D. Bark
Solution
Step 1: Identify method overriding in Dog class
Dog class overrides the sound() method from Animal to print "Bark" instead of "Some sound".
Step 2: Check which method is called
When pet.sound() is called, it uses Dog's version, printing "Bark".