Loading

Quipoin Menu

Learn • Practice • Grow

python / Polymorphism
tutorial

Polymorphism

Polymorphism allows different classes to be treated as instances of the same class through a common interface. Python achieves polymorphism through duck typing ("if it walks like a duck..."), method overriding, and operator overloading.

Duck Typing
An object's suitability is determined by the presence of methods/attributes, not its type.


class Duck:
def sound(self):
return "Quack"

class Dog:
def sound(self):
return "Woof"

def make_sound(animal):
print(animal.sound())

make_sound(Duck()) # Quack
make_sound(Dog()) # Woof

Method Overriding
Subclasses can provide their own implementation of a parent method. This is a form of polymorphism.

Operator Overloading
Python allows you to define how operators (+, -, etc.) work with your objects using magic methods (e.g., `__add__`).


class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
Two Minute Drill
  • Polymorphism allows using objects of different types through a common interface.
  • Duck typing focuses on what an object can do, not its class.
  • Method overriding is a key OOP feature for polymorphic behavior.
  • Operator overloading uses special methods like `__add__`.

Need more clarification?

Drop us an email at career@quipoinfotech.com