Polymorphism
Polymorphism is a programming-language capability that lets a common interface or operation work with values of different types while allowing behavior to vary by type or implementation.
100 C++ Mistakes and How to Avoid Them
Polymorphism is the ability to use a common interface while selecting behavior according to an object's actual type.
- How it is used
- Uses inheritance to treat related objects uniformly while preserving behavior selected by their derived types.
- How deep it goes
- Explains interfaces, pointers, references, slicing, destruction, exceptions, and assignment, but stops short of broader language mechanics.
- Examples
- Demonstrates Person and Student, Square and Horse, Moveable collections, Shape destruction, and derived exception handling.
- What you can do after
- Apply it via C++ inheritance and interfaces while avoiding slicing, invalid substitution, unsafe destruction, and incomplete copying.
Read how 100 C++ Mistakes and How to Avoid Them explains polymorphism →
The Well-Grounded Python Developer: How the pros use Python and Flask
Polymorphism is an object-oriented technique in which the same method call can produce different behavior depending on the object's class or runtime type.
- How it is used
- Keeps a drawing loop usable across different shapes through a shared draw() call.
- How deep it goes
- Defines polymorphism and works through inherited and shape-specific draw() methods; stops short of method-dispatch internals.
- Examples
- Uses Rectangle, Square, and Circle objects in a drawing window, plus a loop that calls draw() on each shape.
- What you can do after
- Apply it via Python classes to draw a mixed collection of shapes without changing the display loop for each shape.
Learn C++ by Example
Polymorphism is the ability to invoke a common interface while allowing the actual object's type to determine the implementation.
- How it is used
- Uses a shared Blob interface to race different movement implementations through base pointers.
- How deep it goes
- Explains abstract interfaces, overriding, v-table dispatch, virtual destructors, and pointer ownership, stopping short of a complete race implementation.
- Examples
- Demonstrates racing stepper and random blobs, configuring uniform or normal movement, and testing with deterministic zero-producing callables.
- What you can do after
- Build polymorphic C++ classes, dispatch through base pointers, combine derived objects, and test implementations with controlled generators.
Data-Oriented Programming: Reduce software complexity
Polymorphism is a language capability in which a common interface can represent values or objects with different implementations.
- How it is used
- Decouples a common interface from varying implementations while supporting object-oriented and data-oriented designs.
- How deep it goes
- Explains interfaces, type dispatch, validation, multimethod structure, and dispatch variants without descending into implementation internals.
- Examples
- Uses dog, cat, and cow greetings, type-based switches, animal maps, schemas, and separate greeting functions.
- What you can do after
- Builds polymorphic behavior with interfaces, type dispatch, or multimethods while evaluating inheritance and data-oriented trade-offs.
Read how Data-Oriented Programming: Reduce software complexity explains polymorphism →
API Design Patterns
Polymorphism is an object-oriented programming technique that allows common interfaces to be used across different concrete types while hiding type-specific implementation details.
- How it is used
- Translates object-oriented shared interfaces into resource-oriented APIs that represent and operate on related concrete resource types.
- How deep it goes
- Explains resource structure, subtype validation, type evolution, behavior, trade-offs, and method restrictions, but not storage implementation.
- Examples
- Uses Shape, Triangle, Square, Message, mixed-type listings, subtype fields, and a countSides API method.
- What you can do after
- Design polymorphic resources, validate subtype data, evolve type strings carefully, and avoid methods that constrain future behavior.
Go by Example
Polymorphism is the ability to treat instances of different types through a shared type or interface while preserving type-specific behavior.
- How it is used
- Uses interfaces and driver abstractions to make database operations independent of specific database technologies.
- How deep it goes
- Distinguishes embedding from inheritance, then explains concrete-type driver polymorphism and optional features via type assertions.
- Examples
- Demonstrates sql.Open with SQLite and PostgreSQL, mediated by *sql.DB and sql/driver interfaces.
- What you can do after
- Apply it via database/sql by using *sql.DB while drivers handle database-specific behavior.
Object-Oriented Software Design in C++
Polymorphism is the ability for an object's behavior to vary according to the concrete subclass of the object being referenced.
- How it is used
- Uses superclass interfaces and runtime dispatch to let callers work with multiple subclass implementations.
- How deep it goes
- Explains signatures, overriding, virtual functions, pointers, and runtime dispatch through a tax example.
- Examples
- Demonstrates GeneralTax, LocalTax, and MaxTax calculating different amounts through GeneralTax pointers.
- What you can do after
- Apply it via C++ virtual functions and subclass overrides to support runtime-varying behavior.
Read how Object-Oriented Software Design in C++ explains polymorphism →
Go in Action, Second Edition
Polymorphism is the ability to treat values of different concrete types through a shared interface or abstraction.
- How it is used
- Uses interfaces to share behavior across domain types such as recipe measurements without inheritance or rigid type hierarchies.
- How deep it goes
- Explains interface contracts and demonstrates concrete measurement conversions, while emphasizing design flexibility rather than runtime internals.
- Examples
- Demonstrates String methods, Convertible measurements, recipe scaling, unit conversion, and grocery-list generation.
- What you can do after
- Apply it via Go interfaces to write flexible code that accepts multiple implementations and supports later domain extensions.
Read how Go in Action, Second Edition explains polymorphism →
Clojure: The Essential Reference
Polymorphism is a programming technique in which one operation can select different implementations according to its inputs or dispatch criteria.
- How it is used
- Supports extensible behavior across Clojure values while interoperating with Java classes, interfaces, and inheritance.
- How deep it goes
- Explains metadata dispatch, multimethods, protocols, and type checks with examples, but defers detailed multimethod mechanics.
- Examples
- Uses metadata-tagged contacts, multimethod formatting, runtime type inspection, and instance? checks against Java classes and interfaces.
- What you can do after
- Apply it via metadata dispatch or multimethods, and recognize when protocols better replace cascading instance? conditionals.
Read how Clojure: The Essential Reference explains polymorphism →
Related concepts
- object-oriented programmingpart of · Object-oriented programming is a programming paradigm that organizes software around objects combining state and behavior, commonly using encapsulation, classes, inheritance, or polymorphism.
- interfaceused by · A software interface is a contract or boundary that specifies the operations, properties, or capabilities through which types or components interact, without requiring a particular implementation.
- trait objectimplemented by · A Rust trait object is a value accessed through a trait interface with its concrete implementing type hidden and methods dispatched dynamically.
- contractuses · An API contract is a specification of the operations, data, and behavioral expectations that govern interactions between software components.
- code to the interface principleused by · A design principle in which code depends on an interface and polymorphic behavior rather than on a specific implementation or subclass.
- polymorphic resourceuses · A polymorphic resource is an API resource whose generic interface can represent multiple specific resource implementations with implementation-dependent behavior.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.








