Recall & Review
beginner
What is polymorphism in the context of inheritance?
Polymorphism means that a child class can use methods from its parent class but also change (override) them to behave differently. It allows one interface to be used for different underlying forms (data types).Click to reveal answer
beginner
How does method overriding demonstrate polymorphism?
Method overriding happens when a child class provides its own version of a method that already exists in the parent class. This lets the same method name do different things depending on the object calling it.Click to reveal answer
beginner
Consider this code snippet:<br><pre>class Animal:
def sound(self):
return "Some sound"
class Dog(Animal):
def sound(self):
return "Bark"
class Cat(Animal):
def sound(self):
return "Meow"</pre><br>What concept does this example show?This example shows polymorphism through inheritance. The method
sound is defined in the parent class Animal and overridden in child classes Dog and Cat to produce different sounds.Click to reveal answer
beginner
Why is polymorphism useful in programming?
Polymorphism lets us write flexible code that can work with different types of objects through a common interface. It helps reuse code and makes programs easier to extend and maintain.
Click to reveal answer
beginner
What happens if a child class does not override a method from its parent?If a child class does not override a method, it inherits the method from the parent class and uses it as is. This means the child class will behave like the parent for that method.Click to reveal answer
What does polymorphism allow in object-oriented programming?
✗ Incorrect
Polymorphism allows methods with the same name to behave differently depending on the object calling them.
In Python, how do you override a method from a parent class?
✗ Incorrect
You override a method by defining a method with the same name in the child class.
If a child class does not override a parent method, what happens when the method is called on a child object?
✗ Incorrect
The child class inherits the parent method and uses it if it does not override it.
Which of these is an example of polymorphism through inheritance?
✗ Incorrect
Polymorphism means same method name, different behavior in different classes.
Why might a programmer use polymorphism?
✗ Incorrect
Polymorphism helps write flexible code that can handle different object types through a common interface.
Explain polymorphism through inheritance in your own words and give a simple example.
Think about how child classes can change parent methods.
You got /4 concepts.
Describe why polymorphism is important for writing flexible and reusable code.
Consider how one method name can work differently for different objects.
You got /4 concepts.