Skip to main content

Polymorphism

Pronunciation
pol-ee-MOR-fiz-um
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/polymorphism

In short

Polymorphism is the ability of code to work with values of different types through one shared interface, with each type supplying its own behavior.

What is polymorphism in programming?

Polymorphism, from the Greek for 'many forms', means the same piece of code can work with objects of different types. You call a method such as area() on a shape without knowing whether it's a circle or a square, and each object responds in its own correct way. This lets you write general code once and add new types later without changing it.

The most common form in object-oriented programming is subtype polymorphism: classes that share a parent class or interface each override the same method, and at runtime the language picks the right version based on the object's actual type, a process called dynamic dispatch. Method overloading, where one class has several methods with the same name but different parameters, is another form that is resolved at compile time. Generics are a third form, called parametric polymorphism, where one function or class works with many types.

A universal remote is a good analogy: pressing the power button works on the TV, the soundbar, and the streaming box, even though each device turns on in its own way. In the same way, a payment system can call pay() on card, bank transfer, or wallet objects alike, and a drawing app can loop over a list of shapes and call draw() on each one.

Polymorphism is often confused with inheritance. Inheritance is one way to share code between classes, while polymorphism is the ability to use different types through a common interface, which can also come from interfaces or from duck typing in dynamic languages like Python and JavaScript, where any object with the right method works. A long if or switch statement that checks an object's type before deciding what to do is a sign that polymorphism would help.

Key takeaways

  • Polymorphism means one interface, many implementations.
  • Subtype polymorphism picks the right overridden method at runtime based on the object's actual type.
  • Method overloading and generics are other forms of polymorphism.
  • Duck typing gives dynamic languages polymorphism without a shared parent class.
  • It replaces long type-checking if or switch chains with a single method call.

Example

One loop, many shapes in JavaScriptjavascript
class Shape {
  area() { return 0; }
}
class Circle extends Shape {
  constructor(r) { super(); this.r = r; }
  area() { return Math.PI * this.r ** 2; }
}
class Square extends Shape {
  constructor(side) { super(); this.side = side; }
  area() { return this.side ** 2; }
}

// One loop works for every kind of shape, including ones added later
const shapes = [new Circle(1), new Square(2)];
for (const s of shapes) console.log(s.area()); // 3.14159..., then 4

Readers ask

What is the difference between polymorphism and inheritance?

Inheritance lets a class reuse and extend another class's code, while polymorphism lets code treat objects of different types through a shared interface. Inheritance is one common way to get polymorphism, but interfaces and duck typing achieve it without sharing any code.

What is the difference between overloading and overriding?

Overloading means several methods in the same class share a name but take different parameters, and the compiler picks one at compile time. Overriding means a subclass replaces a parent's method that has the same signature, and the right version is picked at runtime.

What are the types of polymorphism?

The main types are subtype polymorphism (overriding methods from a shared parent or interface), ad hoc polymorphism (method and operator overloading), and parametric polymorphism (generics). Dynamic languages also rely heavily on duck typing.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings