Complete the code to define a method that can be used by different classes.
class Animal: def sound(self): [1]
The pass statement is used to define an empty method that can be overridden by subclasses, enabling polymorphism.
Complete the code to override the method in the subclass.
class Dog(Animal): def sound(self): [1]('Bark')
return instead of print which won't show output.pass.The print function outputs the dog's sound, overriding the base class method.
Fix the error in the code to call the correct method for each animal.
def animal_sound(animal): [1].sound()
The variable animal is the object, so calling animal.sound() is correct. The blank should be the object animal to call its method.
Fill both blanks to create a list of animals and call their sounds using polymorphism.
animals = [[1], [2]] for a in animals: a.sound()
Animal() which has no sound implementation.Creating instances of Dog and Cat classes allows calling their overridden sound() methods, demonstrating polymorphism.
Fill all three blanks to define classes and demonstrate polymorphism with a function.
class [1]: def sound(self): pass class [2]([1]): def sound(self): print('Meow') def make_sound(animal): animal.sound() make_sound([2]())
The base class Animal defines the method sound. The subclass Cat overrides it. The function make_sound calls the correct method showing polymorphism.