A programming principle enabling objects of different types to share a common interface.
Polymorphism is a core principle in object-oriented programming that allows a single interface, method, or function to operate on objects of different types, producing type-appropriate behavior at runtime or compile time. In practice, this means a method call on a base class reference can invoke different implementations depending on the actual subclass of the object involved. This is achieved through two primary mechanisms: subtype polymorphism (method overriding, where subclasses redefine inherited methods) and parametric polymorphism (generics or templates, where code operates uniformly across types). A third form, ad hoc polymorphism, covers method overloading, where the same method name handles different argument signatures.
In machine learning and AI engineering, polymorphism is pervasive in framework design. Libraries like PyTorch and TensorFlow rely heavily on polymorphic abstractions — a forward() method defined on a base Module class, for instance, is overridden by every custom neural network layer or model. This allows training loops, optimizers, and evaluation pipelines to operate on any model architecture without modification, dramatically simplifying code reuse and experimentation. Similarly, scikit-learn's estimator API uses polymorphism so that any classifier or regressor exposes the same fit() and predict() interface, enabling pipelines to swap models interchangeably.
The practical value of polymorphism in AI systems lies in extensibility and abstraction. Researchers can define new model architectures, loss functions, or data loaders by subclassing existing components, inheriting shared behavior while customizing only what differs. This reduces boilerplate, enforces consistent interfaces, and makes large codebases easier to maintain and test. Dynamic dispatch — the runtime resolution of which method implementation to call — is what makes this flexibility possible without requiring the calling code to know the concrete type of the object it is working with.
While polymorphism is a general software engineering concept, its relevance to ML is particularly high given the scale and modularity of modern deep learning frameworks. Understanding polymorphism helps practitioners read framework source code, design clean experiment pipelines, and build reusable components that integrate seamlessly into existing ecosystems.