Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Polymorphism through inheritance
📖 Scenario: Imagine you are creating a simple program to describe different types of animals and their sounds. Each animal makes a unique sound, but they all share the ability to make a sound.
🎯 Goal: You will build a small program using inheritance where different animal classes share a common method name but behave differently. This shows polymorphism in action.
📋 What You'll Learn
Create a base class called Animal with a method make_sound.
Create two subclasses Dog and Cat that inherit from Animal.
Override the make_sound method in each subclass to print the correct animal sound.
Create instances of Dog and Cat and call their make_sound methods.
💡 Why This Matters
🌍 Real World
Polymorphism helps programmers write flexible code that can work with different types of objects in a uniform way, like handling different animals making sounds without checking their exact type.
💼 Career
Understanding polymorphism and inheritance is essential for software development jobs, especially when designing systems that need to be easily extended and maintained.
Progress0 / 4 steps
1
Create the base class Animal
Create a class called Animal with a method make_sound that prints 'Some generic sound'.
Python
Hint
Use class Animal: to start the class and define make_sound with def make_sound(self):.
2
Create subclasses Dog and Cat
Create two classes called Dog and Cat that inherit from Animal. Do not add any methods yet.
Python
Hint
Use class Dog(Animal): and class Cat(Animal): to inherit from Animal.
3
Override make_sound in Dog and Cat
In the Dog class, override make_sound to print 'Woof!'. In the Cat class, override make_sound to print 'Meow!'.
Python
Hint
Define make_sound inside each subclass with the correct print statement.
4
Create instances and call make_sound
Create an instance called dog of class Dog and an instance called cat of class Cat. Call dog.make_sound() and cat.make_sound() to print their sounds.
Python
Hint
Create objects with dog = Dog() and cat = Cat(). Then call dog.make_sound() and cat.make_sound().
Practice
(1/5)
1. What does polymorphism through inheritance allow in Python?
easy
A. One method name to have different behaviors in child classes
B. Multiple inheritance from unrelated classes
C. Using the same variable name in different functions
D. Creating objects without defining classes
Solution
Step 1: Understand polymorphism concept
Polymorphism means one method name can behave differently depending on the class.
Step 2: Relate to inheritance
Child classes override the method to provide their own behavior.
Final Answer:
One method name to have different behaviors in child classes -> Option A
Quick Check:
Polymorphism = One method, many behaviors [OK]
Hint: Polymorphism means same method, different actions [OK]
Common Mistakes:
Confusing polymorphism with multiple inheritance
Thinking polymorphism means same variable names
Believing objects can exist without classes
2. Which of the following is the correct way to override a method in a child class?
easy
A. Call the parent method inside the child method without redefining
B. Use the keyword override before the method
C. Define a method with the same name in the child class
D. Rename the method in the child class
Solution
Step 1: Check method overriding syntax
In Python, overriding means defining a method with the same name in the child class.
Step 2: Verify other options
Python does not use override keyword; renaming is not overriding; calling parent method alone is not overriding.
Final Answer:
Define a method with the same name in the child class -> Option C
Quick Check:
Override = same method name in child [OK]
Hint: Override by redefining method name in child class [OK]
Common Mistakes:
Using a non-existent override keyword
Thinking calling parent method equals overriding
Renaming method instead of overriding
3. What will be the output of this code?
class Animal:
def sound(self):
return "Some sound"
class Dog(Animal):
def sound(self):
return "Bark"
class Cat(Animal):
def sound(self):
return "Meow"
animals = [Dog(), Cat(), Animal()]
for a in animals:
print(a.sound())
medium
A. Bark\nMeow\nSome sound
B. Some sound\nSome sound\nSome sound
C. Bark\nMeow\nError
D. Error\nError\nError
Solution
Step 1: Identify method overriding
Dog and Cat classes override sound method to return "Bark" and "Meow" respectively.
Step 2: Trace the loop output
Loop calls sound() on Dog(), Cat(), and Animal() objects, printing "Bark", "Meow", and "Some sound".
Final Answer:
Bark
Meow
Some sound -> Option A
Quick Check:
Overridden methods print their own sounds [OK]
Hint: Child method overrides parent, prints child's return [OK]
Common Mistakes:
Assuming parent method always runs
Expecting errors from calling base class method
Mixing output order
4. Find the error in this code that tries to demonstrate polymorphism:
class Vehicle:
def move(self):
print("Moving")
class Car(Vehicle):
def move(self):
print("Driving")
class Bike(Vehicle):
def move(self):
print("Riding")
vehicles = [Car(), Bike()]
for v in vehicles:
v.move
medium
A. List should include Vehicle() instance
B. Incorrect class inheritance syntax
C. Using print instead of return in methods
D. Missing parentheses when calling move method
Solution
Step 1: Check method calls in loop
The code uses v.move without parentheses, so method is not called.
Step 2: Understand effect of missing parentheses
Without parentheses, method object is referenced but not executed, so no output occurs.
Final Answer:
Missing parentheses when calling move method -> Option D
Quick Check:
Method call needs () to execute [OK]
Hint: Always add () to call methods [OK]
Common Mistakes:
Forgetting parentheses on method calls
Thinking print vs return causes error here
Believing inheritance syntax is wrong
5. You want to add a new class Bird that also uses polymorphism with sound(). Which code correctly extends the existing classes and uses polymorphism?
hard
A. class Bird:
def sound(self):
return "Chirp"
animals.append(Bird())
for a in animals:
print(a.sound())
B. class Bird(Animal):
def sound(self):
return "Chirp"
animals.append(Bird())
for a in animals:
print(a.sound())
C. class Bird(Animal):
def noise(self):
return "Chirp"
animals.append(Bird())
for a in animals:
print(a.sound())
D. class Bird(Animal):
def sound(self):
print("Chirp")
animals.append(Bird())
for a in animals:
print(a.sound())
Solution
Step 1: Check inheritance and method name
Bird must inherit from Animal and override sound() method to maintain polymorphism.
Step 2: Verify method behavior and usage
Method returns string "Chirp" like others; appending Bird() to animals list and calling sound() works correctly.
Final Answer:
Correctly inherits Animal and overrides sound() returning "Chirp" -> Option B
Quick Check:
Polymorphism needs same method name and inheritance [OK]
Hint: Inherit and override same method name with matching signature [OK]