Bird
Raised Fist0

What will be the output of this code?

medium📝 Predict Output Q5 of Q15
Python - Inheritance and Code Reuse
What will be the output of this code?
class Bird:
    def fly(self):
        return "Flying"

class Parrot(Bird):
    def fly(self):
        return "Parrot flying"

obj = Parrot()
print(obj.fly())
AError
B"Flying"
C"Bird flying"
D"Parrot flying"
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding

    The Parrot class overrides the fly method of Bird.
  2. Step 2: Method call on child instance

    Calling obj.fly() uses the Parrot class method, returning "Parrot flying".
  3. Final Answer:

    "Parrot flying" -> Option D
  4. Quick Check:

    Child method overrides parent method [OK]
Quick Trick: Child class method overrides parent method [OK]
Common Mistakes:
MISTAKES
  • Assuming parent method is called instead of child
  • Confusing method names or outputs
  • Expecting an error due to inheritance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes