Polymorphism allows code to handle multiple data types through a single unified interface.
In short: In computer science, polymorphism enables a single operation to function across various data types. Borrowed from biology, this concept allows programs to be more flexible and expressive. By using specific mechanisms like subtyping or parametric design, developers can write cleaner, more reusable code that adapts to different inputs seamlessly.
At its core, polymorphism allows a variable or value to possess more than one type, or an operation to execute across multiple types. Christopher Strachey originally categorized the two main classes of this concept as ad hoc and parametric polymorphism. Ad hoc polymorphism, often seen in function or operator overloading, allows functions to behave differently depending on the specific type of argument provided. Conversely, parametric polymorphism allows functions and data types to be written generically, handling values uniformly without needing to know their specific type, a concept formally developed into System F by John C. Reynolds and Jean-Yves Girard.
Subtyping, or inclusion polymorphism, introduces a hierarchy where a name denotes instances of different classes related by a common superclass. As introduced by Peter Wegner and Luca Cardelli in 1985, this allows a function to accept a supertype while correctly handling any of its subtypes. In object-oriented languages, this is typically implemented via subclassing and virtual tables (vtables). When a polymorphic method is called, the system uses late binding and single dispatch to look up the correct implementation at runtime, though some systems like the Common Lisp Object System support multiple dispatch.
The choice between static and dynamic polymorphism involves trade-offs in performance and flexibility. Static polymorphism, common in parametric and ad hoc forms, is resolved at compile time, offering faster execution and better optimization. Dynamic polymorphism, typical of subtyping, provides greater flexibility—such as duck typing—but incurs runtime overhead due to dynamic dispatch. Advanced techniques like template metaprogramming can sometimes bridge these gaps, while modern languages like Swift may prioritize dynamic dispatch by default to manage binary size and library compatibility.
Opens the Intellect+ feed with this card first. Swipe up for the next one.
Sources
- Summary by Intellect · source: Polymorphism (computer science)
To cite this card, link to https://intellectplus.co.uk/app/technology-polymorphism-computer-science.
How we source, check and correct cards: Editorial standards.
General information, not professional advice. Spotted an error? Flag the card or see corrections and complaints.
Intellect Plus is a visual educational discovery platform for exploring fascinating facts and ideas. Brain Food, Not Brain Rot. What is Intellect Plus?
Related
More in Computing and information technology · More in Technology · Latest · A–Z · All topics