Computer ScienceProgrammingMedium

Polymorphism

Also known as:method overridingmethod overloadingdynamic dispatch

Polymorphism is the OOP principle that allows a single interface or method name to represent different underlying implementations depending on the object type. There are two main forms: compile-time polymorphism (method overloading, where multiple methods share a name but differ in parameters) and runtime polymorphism (method overriding, where a subclass provides its own implementation of a parent's method). Polymorphism makes programs more flexible and extensible, allowing new object types to be added without changing existing code.

Compile-Time vs Runtime Polymorphism

FeatureCompile-Time (Overloading)Runtime (Overriding)
Resolution timeAt compilationAt runtime
Keyword usedSame method name, different params@Override annotation (Java)
Binding typeStatic bindingDynamic binding
Exampleadd(int,int) vs add(double,double)Animal.sound() overridden in Dog
PerformanceFaster (resolved early)Slightly slower (resolved late)

Interactive Tools

Codecademy – Polymorphism Lesson

Open Tool

W3Schools – Java Polymorphism

Open Tool

Brilliant – Polymorphism

Open Tool
Diagram illustrating polymorphism with multiple classes implementing a common interface

Wikimedia Commons, CC BY-SA

Related Terms

From Greek "polys" (many) and "morphe" (form), meaning "many forms". In biology, polymorphism refers to multiple forms of a species; the term was applied to programming by Strachey (1967) and formalised by Cardelli and Wegner in their 1985 paper on types.

polymorphismoopoverridingoverloadingdynamic-binding