Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q13 of Q15
Python - Polymorphism and Dynamic Behavior
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()
AHello from Parent
BError: greet method not found
CHello from Parent\nHello from Child
DHello from Child
Step-by-Step Solution
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]
Quick Trick: Child method runs when overridden, not parent's [OK]
Common Mistakes:
MISTAKES
  • Expecting both parent and child messages to print
  • Thinking parent method runs instead of child
  • Assuming error due to method name conflict

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes