0
0
Pythonprogramming~10 mins

Method overriding 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 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'
Agreet
Bsay_hello
Chello
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name in the child class.
Forgetting to define the method in the child class.
2fill in blank
medium

Complete 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'
Amake_sound
Bcall_sound
Cnoise
Dsound
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method name that does not exist.
Using the parent class method name incorrectly.
3fill in blank
hard

Fix 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'
Abegin
Brun
Cstart
Dgo
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name in the child class.
Not overriding the method at all.
4fill in blank
hard

Fill 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'
Awrite
Bstart
Dbegin
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in override and super call.
Not calling the parent method inside the child method.
5fill in blank
hard

Fill 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'
Aprint_message
C"Printing in color"
D"Color print"
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names.
Not calling the parent method.
Incorrect string in print statement.