Polymorphism
Explore how polymorphism enables flexible and dynamic code.
Polymorphism is a free Learn AI with Python lesson on CoddyKit — lesson 4 of 6. 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 Learn AI with Python learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Introduction to Polymorphism
Polymorphism is a core concept in Object-Oriented Programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It provides flexibility and reusability in code.
In this lesson, you’ll learn how to implement polymorphism in Python using methods and classes.

2
What Is Polymorphism?
Polymorphism allows methods in different classes to share the same name while behaving differently. It makes the code more flexible and easier to extend.
# Defining a superclass
class Animal:
# Method in the superclass
def speak(self):
print("Animal speaks") # Generic message for all animals
# Defining a subclass that overrides the speak method
class Dog(Animal):
def speak(self):
print("Dog barks") # Specific behavior for Dog3
4
Polymorphism with Methods
Methods with the same name in different classes can provide different implementations.
# Another subclass with its own implementation of the speak method
class Cat(Animal):
def speak(self):
print("Cat meows") # Specific behavior for Cat
# Function demonstrating polymorphism
# It can accept objects of any subclass of Animal
def animal_sound(animal):
animal.speak() # Calls the appropriate speak method based on the object type
# Creating objects of Dog and Cat
dog = Dog()
cat = Cat()
# Calling the function with different objects
animal_sound(dog) # Outputs: Dog barks
animal_sound(cat) # Outputs: Cat meows4
Polymorphism with Methods
Methods with the same name in different classes can provide different implementations.
# Another subclass with its own implementation of the speak method
class Cat(Animal):
def speak(self):
print("Cat meows") # Specific behavior for Cat
# Function demonstrating polymorphism
# It can accept objects of any subclass of Animal
def animal_sound(animal):
animal.speak() # Calls the appropriate speak method based on the object type
# Creating objects of Dog and Cat
dog = Dog()
cat = Cat()
# Calling the function with different objects
animal_sound(dog) # Outputs: Dog barks
animal_sound(cat) # Outputs: Cat meows5
Polymorphism with Functions
Polymorphism allows the same function to operate on different types of objects.
# Defining two unrelated classes
class Bird:
def fly(self):
print("Bird flies") # Behavior specific to birds
class Plane:
def fly(self):
print("Plane flies") # Behavior specific to planes
# Function demonstrating polymorphism
# It can accept objects of both Bird and Plane
def flying_object(obj):
obj.fly() # Calls the appropriate fly method based on the object type
# Creating objects of Bird and Plane
bird = Bird()
plane = Plane()
# Calling the function with different objects
flying_object(bird) # Outputs: Bird flies
flying_object(plane) # Outputs: Plane flies6
Polymorphism with Inheritance
Polymorphism is often achieved through inheritance, where subclasses override methods from the superclass.
# Superclass representing a generic vehicle
class Vehicle:
def start(self):
print("Vehicle starts") # Generic behavior for all vehicles
# Subclass representing a specific type of vehicle
class Car(Vehicle):
def start(self):
print("Car starts") # Specific behavior for cars
# Subclass representing another specific type of vehicle
class Bike(Vehicle):
def start(self):
print("Bike starts") # Specific behavior for bikes
# Function demonstrating polymorphism
# It can accept objects of any subclass of Vehicle
def start_vehicle(vehicle):
vehicle.start() # Calls the appropriate start method based on the object type
# Creating objects of Car and Bike
car = Car()
bike = Bike()
# Calling the function with different objects
start_vehicle(car) # Outputs: Car starts
start_vehicle(bike) # Outputs: Bike starts7
Advantages of Polymorphism
Polymorphism offers several benefits:
- Code reusability and flexibility.
- Improved code readability and organization.
- Encourages the use of common interfaces.
8
Real-World Example
Here’s a real-world example using polymorphism.
# Real-world example of polymorphism
class Developer:
def work(self):
print("Writing code") # Specific task for developers
class Designer:
def work(self):
print("Designing user interfaces") # Specific task for designers
# Function demonstrating polymorphism
# It can accept objects of both Developer and Designer
def workday(employee):
employee.work() # Calls the appropriate work method based on the object type
# Creating objects of Developer and Designer
developer = Developer()
designer = Designer()
# Calling the function with different objects
workday(developer) # Outputs: Writing code
workday(designer) # Outputs: Designing user interfaces9
10
Common Mistakes with Polymorphism
Here are some mistakes to avoid:
- Forgetting to override methods in subclasses when required.
- Confusing method resolution order (MRO) in multiple inheritance.
- Using polymorphism unnecessarily for simple tasks.
11
What Did We Learn?
In this lesson, you learned:
- What polymorphism is and how it works in Python.
- How to implement polymorphism with methods and inheritance.
- The advantages of using polymorphism in OOP.
Great job! Let’s move to the next topic.

Frequently asked questions
Is the “Polymorphism” lesson free?
Yes — the full text of “Polymorphism” is free to read here on the web, and the Learn AI with Python course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.
What will I learn in “Polymorphism”?
Explore how polymorphism enables flexible and dynamic code. You practise Learn AI with Python 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 Learn AI with Python?
No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 6, 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 Learn AI with Python lesson?
Yes. Every Learn AI with Python 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.