What is polymorphism?

Polymorphism is one of the four fundamental OOP principles. It is the ability to present the same interface for differing underlying types. In terms of .NET, for example, polymorphism means that at run time, objects of a subclass may be treated as objects of a superclass in places of their use, such as collections or method parameters. When this occurs, the object's actual type at run-time is no longer identical to its declared type. There are several kinds of polymorphism.

Types of polymorphism

  • Subtyping (also called subtype polymorphism or inclusion polymorphism) is most known type: it is the ability to use child classes through base class references. In the object-oriented programming community, this is often simply referred to as polymorphism.
  • Ad hoc polymorphism allows functions with the same name act differently for each type. Ad hoc polymorphism is supported in many languages using function overloading.
  • Parametric polymorphism: when code is written without mention of any specific type and thus can be used transparently with any number of new types. In the object-oriented programming community, this is often known as compile-time polymorphism, or generics, or generic programming. In the functional programming community, this is often shortened to polymorphism.

Short answer

  • Polymorphism is one of the four fundamental concepts in OOP.
  • Polymorphism is the ability of an object of a subclass to be treated as object of a superclass.
  • There three types of polymorphism: subtyping, ad hoc polymorphism and parametric polymorphism.

Comments