0
0
Pythonprogramming~10 mins

Inheriting attributes and methods in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to make class Dog inherit from class Animal.

Python
class Animal:
    def __init__(self, name):
        self.name = name

class Dog([1]):
    pass
Drag options to blanks, or click blank then click option'
AAnimal
BDog
Cobject
DPet
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the parent class name in parentheses.
Using the child class name instead of the parent class name.
2fill in blank
medium

Complete the code to call the parent class constructor inside the child class.

Python
class Animal:
    def __init__(self, name):
        self.name = name

class Dog(Animal):
    def __init__(self, name, breed):
        [1](name)
        self.breed = breed
Drag options to blanks, or click blank then click option'
Asuper().__init__
BAnimal.__init__
CDog.__init__
Dself.__init__
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.__init__ which causes infinite recursion.
Calling the child class constructor instead of the parent.
3fill in blank
hard

Fix the error in the code to correctly inherit and use the method from the parent class.

Python
class Animal:
    def speak(self):
        return "Animal sound"

class Cat(Animal):
    def speak(self):
        return [1]
Drag options to blanks, or click blank then click option'
Aself.speak() + " meow"
BAnimal.speak() + " meow"
Csuper().speak() + " meow"
D"meow"
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.speak() inside speak causes infinite recursion.
Calling the parent method without super() and missing the instance.
4fill in blank
hard

Fill both blanks to create a child class that inherits and extends the parent class method.

Python
class Vehicle:
    def description(self):
        return "Vehicle"

class Car([1]):
    def description(self):
        return super().[2]() + " - Car"
Drag options to blanks, or click blank then click option'
AVehicle
Bdescription
Cdesc
DCar
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong parent class name.
Calling a method name that does not exist in the parent.
5fill in blank
hard

Fill all three blanks to create a child class that inherits attributes and overrides a method using the parent method.

Python
class Person:
    def __init__(self, name):
        self.name = name
    def greet(self):
        return f"Hello, I am {self.name}"

class Student([1]):
    def __init__(self, name, student_id):
        [2](name)
        self.student_id = student_id
    def greet(self):
        return [3]() + f", my student ID is {self.student_id}"
Drag options to blanks, or click blank then click option'
APerson
Bsuper().__init__
Csuper().greet
Dself.__init__
Attempts:
3 left
💡 Hint
Common Mistakes
Calling self.__init__ instead of super().__init__.
Not calling the parent constructor at all.
Not using super() to call the parent method in greet.