Bird
0
0

Which of the following is the correct way to override a method named greet in a child class?

easy📝 Syntax Q12 of 15
Python - Polymorphism and Dynamic Behavior
Which of the following is the correct way to override a method named greet in a child class?
Adef greet(self, extra):\n print('Hello from child')
Bdef greet(self):\n print('Hello from child')
Cdef greet_child(self):\n print('Hello from child')
Ddef greet():\n print('Hello from child')
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes