Polymorphism
- In Turkish
- Çok biçimlilik
- Pronunciation
- pol-ee-MOR-fiz-um
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
iforswitchchains with a single method call.
Example
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 4Readers 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
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- InheritanceProgramming Fundamentals, p. 28Inheritance is an object-oriented programming feature that lets a new class reuse, extend, and override the fields and methods of an existing class.
- InterfaceProgramming Fundamentals, p. 29An interface is a named set of method and property signatures that a type promises to provide, without saying how those members are implemented.
- ClassProgramming Fundamentals, p. 9A class is a blueprint in object-oriented programming that defines the data and behavior shared by a group of objects, which are created from it as instances.
- GenericsProgramming Fundamentals, p. 24Generics are a language feature that lets you write functions, classes, and types that work with many data types while still keeping full type safety.
- EncapsulationProgramming Fundamentals, p. 17Encapsulation is the practice of bundling data with the methods that use it and hiding internal details, so outside code works only through a public interface.
Spotted a mistake or something missing on this page?Suggest an edit