Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to override the method greet in the child class.
Python
class Parent: def greet(self): print("Hello from Parent") class Child(Parent): def [1](self): print("Hello from Child") c = Child() c.greet()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name in the child class.
Forgetting to define the method in the child class.
✗ Incorrect
The child class overrides the method by defining a method with the same name
greet.2fill in blank
mediumComplete the code to call the overridden method from the child class instance.
Python
class Animal: def sound(self): print("Some sound") class Dog(Animal): def sound(self): print("Bark") pet = Dog() pet.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method name that does not exist.
Using the parent class method name incorrectly.
✗ Incorrect
The method
sound is overridden in the Dog class and called on the instance.3fill in blank
hardFix the error in the child class method overriding by completing the method name correctly.
Python
class Vehicle: def start(self): print("Vehicle started") class Car(Vehicle): def [1](self): print("Car started") my_car = Car() my_car.start()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name in the child class.
Not overriding the method at all.
✗ Incorrect
The child class must override the method with the exact same name
start to replace the parent method.4fill in blank
hardFill both blanks to override the method and call the parent method inside the child method.
Python
class Writer: def write(self): print("Writing...") class Author(Writer): def [1](self): print("Author starts writing") super().[2]() a = Author() a.write()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in override and super call.
Not calling the parent method inside the child method.
✗ Incorrect
The child class overrides
write and calls the parent write method using super().5fill in blank
hardFill all three blanks to override the method, call the parent method, and add extra behavior.
Python
class Printer: def print_message(self): print("Printing message") class ColorPrinter(Printer): def [1](self): super().[2]() print([3]) cp = ColorPrinter() cp.print_message()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names.
Not calling the parent method.
Incorrect string in print statement.
✗ Incorrect
The child class overrides
print_message, calls the parent method, and adds a new print statement.