Polymorphism
Explore advanced OOP concepts.
Polymorphism is a free Python For Kids lesson on CoddyKit — lesson 5 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python For Kids learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Polymorphism
Welcome back to the Object-Oriented Programming (OOP) lesson! Today, we'll explore polymorphism in Python, a powerful concept that allows objects to take on multiple forms.

2
What is Polymorphism?
Polymorphism is a fundamental concept in OOP that allows objects of different classes to be treated as objects of a common superclass. It enables the same method to behave differently based on the object that invokes it.
- Definition: "Many forms" - the ability of different objects to respond uniquely to the same method call.
- Purpose: Enhances flexibility and integration of code by allowing interchangeable objects.
Let's see how polymorphism works in Python!
3
Implementing Polymorphism
Polymorphism can be achieved in Python through method overriding and using built-in functions that work with different object types.
- Method Overriding: Redefining a method in a child class that was already defined in the parent class.
- Built-in Functions: Functions like
len()that work with various data types.
Let's look at method overriding in detail.
4
Method Overriding
Method overriding allows a child class to provide a specific implementation of a method that is already defined in its parent class. This is a key aspect of polymorphism.
Example:
# Defines the Animal parent class with a speak method
class Animal:
def speak(self):
print("The animal makes a sound.") # Generic speak method
5
Creating Child Classes with Overridden Methods
Let's create child classes that inherit from Animal and override the speak method to provide specific behaviors.
Example:
# Defines the Dog child class inheriting from Animal
class Dog(Animal):
def speak(self):
print("The dog barks.") # Overridden speak method
# Defines the Cat child class inheriting from Animal
class Cat(Animal):
def speak(self):
print("The cat meows.") # Overridden speak method
6
Using Polymorphism with Overridden Methods
Now, let's create objects of the child classes and see how they exhibit different behaviors using the same method name.
Example:
# Creates objects of Dog and Cat classes
dog = Dog()
cat = Cat()
# Calls the speak method on both objects
dog.speak() # Output: The dog barks.
cat.speak() # Output: The cat meows.
7
Polymorphism with Built-in Functions
Polymorphism isn't limited to user-defined classes. Python's built-in functions like len() work with different data types, demonstrating polymorphic behavior.
Example:
# Uses len() with a list
my_list = [1, 2, 3, 4]
print(len(my_list)) # Output: 4
# Uses len() with a string
my_string = "Hello"
print(len(my_string)) # Output: 5
# Uses len() with a dictionary
my_dict = {"a": 1, "b": 2}
print(len(my_dict)) # Output: 2
8
Example Program: Shape Area Calculation
Let's create a program that calculates the area of different shapes using polymorphism. We'll define a parent class Shape and child classes Circle and Rectangle that override the area method.
Code:
# Defines the Shape parent class with an area method
class Shape:
def area(self):
pass # Placeholder method
# Defines the Circle child class inheriting from Shape
class Circle(Shape):
def __init__(self, radius):
self.radius = radius # Sets the radius of the circle
def area(self):
return 3.14 * self.radius ** 2 # Calculates area of the circle
# Defines the Rectangle child class inheriting from Shape
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width # Sets the width of the rectangle
self.height = height # Sets the height of the rectangle
def area(self):
return self.width * self.height # Calculates area of the rectangle
# Creates objects of Circle and Rectangle classes
circle = Circle(5)
rectangle = Rectangle(4, 6)
# Calculates and prints areas using polymorphism
print("Circle Area:", circle.area()) # Output: Circle Area: 78.5
print("Rectangle Area:", rectangle.area()) # Output: Rectangle Area: 24
9
# Defines the Vehicle parent class with a move method
class Vehicle:
def move(self):
print("The vehicle moves.")
# Defines the Bicycle child class inheriting from Vehicle
class Bicycle(Vehicle):
def move(self):
print("The bicycle pedals forward.")
# Defines the Airplane child class inheriting from Vehicle
class Airplane(Vehicle):
def move(self):
print("The airplane flies in the sky.")
# Creates objects of Bicycle and Airplane classes
bike = Bicycle()
plane = Airplane()
# Calls the move method on both objects
bike.move()
plane.move()
10
Great Job!
You've learned about polymorphism in Python and how it allows objects of different classes to respond uniquely to the same method calls. Polymorphism enhances the flexibility and scalability of your code, making it easier to manage and extend. Keep practicing by creating more classes and methods that utilize polymorphic behavior to strengthen your understanding!

Frequently asked questions
Is the “Polymorphism ” lesson free?
Yes — the full text of “Polymorphism ” is free to read here on the web, and the Python For Kids course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python For Kids course, upgrade to CoddyKit PRO.
What will I learn in “Polymorphism ”?
Explore advanced OOP concepts. You practise Python For Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python For Kids?
No prior experience is required. Python For Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 5 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Polymorphism ” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python For Kids lesson?
Yes. Every Python For Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.