Method overriding lets a child class change how a method works from its parent class. This helps customize behavior without changing the original code.
Method overriding behavior in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
class Parent: def method(self): # original behavior pass class Child(Parent): def method(self): # new behavior overriding parent pass
The child class uses the same method name to replace the parent's method.
You can still call the parent's method inside the child using super() if needed.
Examples
Dog class changes the sound method to print "Bark" instead of "Some sound".Python
class Animal: def sound(self): print("Some sound") class Dog(Animal): def sound(self): print("Bark")
Car class overrides start but also calls the parent's start using super().Python
class Vehicle: def start(self): print("Vehicle starting") class Car(Vehicle): def start(self): super().start() print("Car engine running")
Sample Program
This program shows how the Child class changes the greet method. Calling greet on Parent prints one message, and calling it on Child prints another.
Python
class Parent: def greet(self): print("Hello from Parent") class Child(Parent): def greet(self): print("Hello from Child") p = Parent() c = Child() p.greet() c.greet()
Important Notes
Method overriding only works if the method names are exactly the same.
Use super() to keep the original behavior and add new actions.
Overriding helps make your code flexible and easier to update later.
Summary
Method overriding lets child classes change parent class methods.
It uses the same method name in the child class.
Use super() to call the parent method if needed.
Practice
1. What does method overriding allow a child class to do in Python?
easy
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 CQuick Check:
Method overriding = change inherited method behavior [OK]
Hint: Overriding means child changes parent's method behavior [OK]
Common Mistakes:
- Thinking overriding creates a new method with a different name
- Believing overriding disables parent method everywhere
- 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
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 BQuick 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
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 DQuick 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
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 AQuick 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
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 AQuick Check:
super() calls parent method before child code [OK]
Hint: super() runs parent method before child code [OK]
Common Mistakes:
- Expecting only child's message to print
- Thinking super() causes error without arguments
- Ignoring order of print statements
