Polymorphism in Python

1) Override a Method in Subclasses

Create a base class Animal with method sound(). Create subclasses Dog and Cat; override sound() so each prints a different string. Instantiate both and call sound().


2) Polymorphism in a Loop

Define classes Car and Bike with .move() methods. In a list [Car(), Bike()], loop and call move() for each object.


3) Same Method Name, Different Behavior

Create classes CalculatorV1 and CalculatorV2, both with add(a, b) but with different print styles. Call add() on both.


4) Polymorphic Function

Write a function describe(obj) that calls obj.describe() for any object with that method.

Test with two different classes implementing describe().


5) Duck Typing

Write a function make_sound(animal) that calls .sound(). Create two different classes with a sound() method (no inheritance) and pass them to make_sound().


6) Built-in Polymorphism: len()

Call len() on a list, string, and dictionary; print results.


7) Operator Polymorphism with +

Use + to add two strings, two lists, and two numbers; print results.


8) Polymorphism with Inheritance

Create a base class Shape with method area(). Subclass Square and Circle override area(). In a list loop, call area() generically.


9) Method Overloading (Simple Simulation)

Write a class with a method show(arg=None). If arg is provided, print it; otherwise print default.


10) Override __str__

Create two classes — both implement __str__. Print instances and show different string formats.


11) Polymorphism Using Abstract Base Class

(Optional for beginners) Create a base class with empty work() method and subclasses override it. Loop and call work().


12) Shape Perimeter

Add method perimeter() to Square and Rectangle classes and call generically in a loop.


13) Polymorphism with Functions

Write function process(item) that prints item info (length if possible, else print item). Test with a string and a number.


14) Animal Polymorphism with Extra Methods

Create class Bird and class Fish; both have a move() method. In a list call move() for both.


15) Polymorphism with Lists of Objects

Create several classes with a common info() method. Store objects in a list and call .info() on each.


16) Read Different Types with Same Function

Write a function print_value(x) that prints whether x is a string, list, or number — call with different types.


17) Polymorphic Math Operation

Define two classes, each implementing calculate(a, b):

  • Class1 returns a + b

  • Class2 returns a * b Call .calculate() on both.


18) Reverse Polymorphism

Write two classes with a method reverse():

  • One reverses a list

  • One reverses a string Call both using a shared function.


19) Polymorphism with Default Arguments

Define two classes, both with method greet(name=None) but different default messages. Call with and without arguments.


20) Polymorphic Sorting

Given a list of objects with a .key() method, sort them using:


🧠 Concepts Practiced

Topic
What You Learn

Method overriding

Different behavior in subclasses

Duck typing

No inheritance needed

Built-in polymorphism

len(), + operator

Generic functions

Work with any object with required method

__str__ polymorphism

Customize printing

Shared interfaces

Common method names across classes


📌 Example Starter for #1


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated