Skip to main content
Definition Website Computer and Technology

Polymorphism

Subject: Computer and Technology noun /ˌpɑː.liˈmɔːr.fɪ.zəm/ 1 definition

  1. Definition (noun)

    Entry #86 · Updated

    Polymorphism is a programming language and type-system principle that allows a single interface, function, method, or operator to operate uniformly across values of multiple distinct types. Rather than duplicating logic or writing explicit type-checking branches for every concrete data representation, callers program against a shared abstraction while the compiler or runtime dispatches each operation to the type-appropriate implementation. Its core boundary is decoupling high-level contract behavior from concrete internal state—requiring participating types to honor behavioral substitutability rather than exposing type-specific implementation details to callers.

    Common Forms

    • Subtype (Inclusion) Polymorphism: Allows code written against a parent class or interface contract to accept any derived subtype interchangeably, typically resolving overridden methods at runtime through dynamic dispatch mechanisms such as virtual method tables in Java and C++.

    • Parametric Polymorphism: Enables functions, classes, and data structures to be defined with abstract type parameters rather than concrete types, allowing one statically type-checked implementation to work across arbitrary data types (such as generics in Rust, Go, and TypeScript or templates in C++).

    • Ad Hoc Polymorphism: Maps a single function name or operator symbol to distinct, type-specific implementations based on argument types, commonly realized through function overloading, operator overloading, or traits and type classes in languages like C++, Haskell, and Rust.

    • Structural Polymorphism: Determines compatibility from the methods or fields a value provides rather than an explicit inheritance declaration, allowing unrelated types with matching shapes to be used uniformly—statically in Go interfaces and TypeScript, or dynamically via duck typing in Python.

    Example

    An e-commerce checkout service defines a PaymentProcessor interface with a charge(amount) method implemented separately by StripeCardProcessor, PayPalProcessor, and ApplePayProcessor. When a customer submits an order, the checkout handler invokes processor.charge(total) without conditional branching per provider, relying on runtime polymorphism to dispatch the call to the selected payment implementation.