Bird
0
0

Identify the issue in the following code that attempts to override a method:

medium📝 Debug Q6 of 15
Python - Inheritance and Code Reuse

Identify the issue in the following code that attempts to override a method:

class Animal:
    def make_sound(self):
        print('Generic sound')

class Cat(Animal):
    def make_sound():
        print('Meow')

pet = Cat()
pet.make_sound()
AThe print statement syntax is incorrect.
BThe parent class method 'make_sound' should be private.
CThe method name 'make_sound' cannot be used in the child class.
DThe overridden method 'make_sound' in Cat is missing the 'self' parameter.
Step-by-Step Solution
Solution:
  1. Step 1: Check method signature

    The method 'make_sound' in the child class must include 'self' as its first parameter to correctly override the parent method.
  2. Step 2: Understand instance method requirements

    Instance methods require 'self' to access instance attributes and to be called on an instance.
  3. Final Answer:

    The overridden method 'make_sound' in Cat is missing the 'self' parameter. -> Option D
  4. Quick Check:

    Method signatures must match for overriding [OK]
Quick Trick: Always include 'self' in instance method definitions [OK]
Common Mistakes:
  • Forgetting 'self' in overridden methods
  • Trying to override with different method names
  • Assuming print syntax errors cause overriding issues

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes