Menu
Coddy logo textTech

What Is Polymorphism?

In programming, polymorphism is the ability of one piece of code, such as a function call or a method name, to work with values of different types, each type supplying its own behavior. Calling shape.area() runs a different calculation for a circle than for a square.

By Kevin Spektor, Co-founder & CTO

Updated September 24, 2026

In Python, len("hello") returns 5, len([4, 8, 15]) returns 3, and len({"a": 1}) returns 1. It is one function name used on three types, and each type counts its own way: a string counts characters, a list counts items, a dictionary counts keys. The word comes from Greek: poly, many, and morph, form. One call, many forms of behavior.

How polymorphism works

Code calls a method by name, such as shape.area(). When that line runs, the language looks at the actual object and finds area on the object's own class. This lookup is called dynamic dispatch, and it is what lets one line do different work for different objects:

Circle 3.14
Square 9
Circle 12.57

Python finds the method by searching the object's class first, then its parent classes in a fixed order called the method resolution order. If Square had no area() of its own, the search would reach Shape.area() and raise NotImplementedError, a signal that every subclass must supply one.

The loop never checks what kind of shape it holds. To add a triangle, you write a Triangle class with its own area(), and the loop works unchanged. Without polymorphism, the loop would need an if isinstance(...) branch for every shape, edited each time a new one appears.

Types of polymorphism

TypeAlso calledWhat it looks likeWhen the version is chosen
SubtypeRuntime polymorphism, method overridingA subclass replaces a method of its parentWhile the program runs
Ad hocOverloadingOne name, several versions for different parameter types; also operator overloadingAt compile time in Java and C++
ParametricGenerics, templatesOne piece of code that works for any type: List<T> in Java, vector<T> in C++At compile time

When people say "polymorphism" in object-oriented programming without more detail, they almost always mean the first row: subtype polymorphism through overriding.

Python has all three in its own way. Overriding works as in the shapes example. Operators such as + are ad hoc polymorphism. Functions like max() and sorted() are parametric in spirit: they accept a list of numbers, strings or dates, as long as the items can be compared, and type hints such as list[int] or a TypeVar can state that explicitly.

Compile-time vs runtime polymorphism in Java

Java has both kinds, and interview questions often ask about the difference:

class Animal { String sound() { return "..."; } }
class Dog extends Animal {
    @Override String sound() { return "Woof"; }
}

Animal pet = new Dog();
pet.sound();          // "Woof": chosen at run time from the object

int add(int a, int b)          { return a + b; }
double add(double a, double b) { return a + b; }
add(2, 3);            // int version, chosen by the compiler
add(2.5, 1.0);        // double version

The variable pet has the type Animal, yet the Dog version runs, because the choice depends on the object, not the variable. The two add methods are overloading: the compiler picks one from the argument types before the program runs. Interfaces give the same runtime behavior without sharing code: any class that implements Comparable can be sorted by Collections.sort. C++ works the same way with one difference: a method gets runtime dispatch only if it is declared virtual. See polymorphism in Java, method overloading and C++ virtual functions.

Polymorphism in Python: duck typing and operators

Python does not require a shared parent class. If an object has the method the code calls, it works: "if it walks like a duck and quacks like a duck, it is a duck". Operators are polymorphic too. Writing a + b calls a.__add__(b), so your own classes can support +, len() and print():

3 abcd [1, 2]
Vector(11, 22)
5 3 2

Inheritance is one way to get polymorphism, not a requirement. Vector inherits from nothing special, yet len() and + treat it like a built-in type. The Python inheritance docs show the subclass route.

Common mistakes

Expecting Python to overload by signature. A second def with the same name silently replaces the first:

Hi Ana Silva
Traceback (most recent call last):
  ...
TypeError: greet() missing 1 required positional argument: 'last'

Use a default argument instead: def greet(first, last="").

Forgetting virtual in C++. Without it, calling a method through a pointer to the base class runs the base version, even when the object is a subclass.

Overrides that change the meaning. If area() on one subclass returns the perimeter, every caller that trusts area() breaks. An override must keep the promise the parent made; this rule is known as the Liskov substitution principle.

Type checks everywhere. A chain of if type(x) == ... branches is usually a sign that each type should carry its own method instead.

Where to go next

Polymorphism works together with encapsulation, which protects each object's data, and abstraction, which defines the shared interface the types implement. Practice overriding in the Python classes docs, or try the Java course, where overloading and overriding appear side by side.

Frequently Asked Questions

What is an example of polymorphism?
The + operator in Python: 1 + 2 adds numbers, "ab" + "cd" joins strings and [1] + [2] joins lists. The same symbol works on three types, and each type decides what it means. In object-oriented code, the classic example is a list of shapes where each one answers area() with its own formula.
What is the difference between polymorphism and inheritance?
Inheritance lets a class reuse the code of a parent class. Polymorphism lets code treat different types through one shared interface. Inheritance is one common way to get polymorphism, because a subclass can override a parent's method, but Python's duck typing and Java's interfaces give polymorphism without sharing any code.
What is polymorphism in biology?
In biology, polymorphism means that two or more forms of a trait or a gene exist in the same population, such as the A, B, AB and O blood types in humans. It shares the Greek root, "many forms", with the programming term, but the two concepts are unrelated.
Does Python support method overloading?
Not by parameter types or counts, as Java and C++ do. A second def with the same name replaces the first one. Python code gets the same effect with default arguments, *args, or functools.singledispatch, which picks a function based on the type of the first argument.
Why is polymorphism useful?
It lets you add new types without changing the code that uses them. A drawing program that calls shape.draw() on every shape needs no edits when you add a triangle class; only the new class is written. Without polymorphism, that loop would grow a new if branch for every type.
Coddy programming languages illustration

Learn to code with Coddy

GET STARTED