Bird
0
0

You want to extend a parent class __init__ method to add a new attribute in the child class. Which code correctly uses super() to do this?

hard📝 Application Q15 of 15
Python - Inheritance and Code Reuse
You want to extend a parent class __init__ method to add a new attribute in the child class. Which code correctly uses super() to do this?
class Animal:
    def __init__(self, name):
        self.name = name

class Dog(Animal):
    def __init__(self, name, breed):
        ???
        self.breed = breed
Choose the correct replacement for ???.
Asuper(Dog, self).__init__(breed)
Bsuper().__init__(name)
CAnimal.__init__(self, breed)
Dsuper().__init__(breed)
Step-by-Step Solution
Solution:
  1. Step 1: Understand parent __init__ parameters

    Animal's __init__ takes name, so we must pass name to it.
  2. Step 2: Use super() correctly in child __init__

    Calling super().__init__(name) runs Animal's __init__ properly, then child adds breed.
  3. Final Answer:

    super().__init__(name) -> Option B
  4. Quick Check:

    super() calls parent with correct args = B [OK]
Quick Trick: Pass parent's expected args to super().__init__() [OK]
Common Mistakes:
  • Passing wrong argument to super()
  • Calling parent __init__ without self
  • Using old super() syntax incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes