Bird
Raised Fist0
Pythonprogramming~10 mins

Method overriding behavior in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - Method overriding behavior
Define Parent class with method
Define Child class overriding method
Create Child object
Call method on Child object
Child's method runs, not Parent's
End
This flow shows how a child class replaces a parent's method with its own when called on a child object.
Execution Sample
Python
class Parent:
    def greet(self):
        print("Hello from Parent")

class Child(Parent):
    def greet(self):
        print("Hello from Child")

c = Child()
c.greet()
This code defines a parent and child class with the same method name; calling the method on the child object runs the child's version.
Execution Table
StepActionEvaluationResult
1Define class Parent with method greetNo outputParent.greet method ready
2Define class Child overriding greetNo outputChild.greet method ready, overrides Parent.greet
3Create object c of class ChildNo outputObject c created as Child instance
4Call c.greet()Look for greet in Child firstFound Child.greet
5Execute Child.greet()Print statement runsOutput: Hello from Child
6End of programNo further callsProgram ends
💡 Method call resolved to Child.greet, overriding Parent.greet; program ends after print
Variable Tracker
VariableStartAfter Step 3Final
cundefinedChild instance createdChild instance
Key Moments - 2 Insights
Why does calling c.greet() run the Child's method, not the Parent's?
Because in step 4 of the execution_table, Python looks for the method in the Child class first and finds the overridden greet method there.
What if the Child class did not have a greet method?
Then Python would look up to the Parent class and run Parent.greet instead, as shown by the method resolution order.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, where does Python find the greet method to run?
AIn the Parent class
BIn the Child class
CIt creates a new method
DIt raises an error
💡 Hint
Check the 'Evaluation' column in step 4 of the execution_table
At which step is the Child object created?
AStep 3
BStep 1
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column for object creation in the execution_table
If the Child class did not override greet, what would be the output at step 5?
AHello from Child
BNo output
CHello from Parent
DError: method not found
💡 Hint
Refer to the key_moments explanation about method lookup when Child does not override
Concept Snapshot
Method overriding lets a child class replace a parent's method.
When calling a method on a child object, Python uses the child's version if it exists.
If not, it uses the parent's method.
This allows customizing behavior in subclasses easily.
Full Transcript
This visual trace shows how method overriding works in Python. First, a Parent class defines a greet method. Then a Child class overrides this greet method with its own version. When we create an object c of Child and call c.greet(), Python looks for greet in Child first and finds the overridden method. It runs Child's greet, printing 'Hello from Child'. If Child did not override greet, Python would run Parent's greet instead. This behavior lets subclasses customize or replace methods from their parent classes.

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

  1. 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.
  2. Step 2: Identify what overriding changes

    The child class method replaces the parent's method behavior when called on the child instance.
  3. Final Answer:

    Change the behavior of a method inherited from the parent class -> Option C
  4. Quick 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
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

  1. Step 1: Match method name exactly

    Overriding requires the child method to have the same name as the parent method, here 'greet'.
  2. Step 2: Check method signature

    The method must include 'self' as the first parameter to be a proper instance method.
  3. Final Answer:

    def greet(self):\n print('Hello from child') -> Option B
  4. 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

  1. Step 1: Identify method overriding

    The Child class defines its own greet method, overriding Parent's greet.
  2. Step 2: Determine which method is called

    Calling obj.greet() on a Child instance calls the Child's greet method, printing 'Hello from Child'.
  3. Final Answer:

    Hello from Child -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Missing self parameter in Child's show method -> Option A
  4. 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

  1. 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.
  2. Step 2: Follow the print statements

    First, 'Hello from Parent' is printed, then 'Hello from Child' is printed after.
  3. Final Answer:

    Hello from Parent\nHello from Child -> Option A
  4. Quick 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