POLYMORPHISM

Polymorphism:-

Polymorphism is a technique through which a single definition can be used with different data types, acting differently each time depending on the data type. Polymorphism is implemented in different forms by many programming languages and paradigms in which the most famous are the object-oriented languages, especially C++. Polymorphism applies to data types as well as functions. A function that can evaluate to and be applied to values of different types is known as a polymorphic function. A data type that contains elements of different types is known as a polymorphic data type.

Types of Polymorphism:-

There are various types of polymorphism that can be identified as follows:

(1) Parametric polymorphism

When a function is parametrically polymorphic, the function can deal equally well with any objects irrespective of their type. In programming, parametric polymorphism was first seen in ML in 1976. Today, parametric polymorphism exists in languages such as Standard ML (or SML), Haskell, and others. Parametric polymorphism makes a language more expressive, while still maintaining full static type-safety.

(2) Predicative and Impredicative polymorphism

Systems that have parametric polymorphism can be predicative and impredicative systems. When a function is impredicative, the same function works irrespective of the types of data passed to it with an added functionality that the data passed to it may itself be polymorphic. In a predicative system, a function that is polymorphic cannot be executed with polymorphic data as arguments. It is this restriction that makes distinguishing between polymorphic and non-polymorphic types very important.

Polymorphism in the language ML and its close relatives is predicative.

(3) Subtyping polymorphism

Some programming languages use subtypes to restrict the range of types that can be used in a particular case of parametric polymorphism. Subtyping polymorphism is also known as dynamic polymorphism or dynamic typing and it allows a function to be written to take an object of a particular type T and can also work when passed an object that belongs to a type S which is a subtype of T. This has been stated in the Liskov substitution principle.

Object-oriented programming languages implement subtyping polymorphism by using inheritance.

(4) Ad-hoc polymorphism

Ad-hoc polymorphism usually means simple overloading, but also automatic type conversion (called coercion). Under Ad-hoc polymorphism, the programmer has to specify exactly what types are to be usable with a polymorphic function.

It maybe understood in this way that for the user there exists only one function that can act differently based on the inputs it is given while in reality, there are several functions with the same name but different arguments. Thus, ad hoc polymorphism is essentially a dispatch mechanism where code moves through one named function and is dispatched to other functions without having to specify the exact function being called.

(5) Overloading

Under overloading, multiple functions can have the same name (but take in different types). It is the responsibility of the compiler or interpreter of the language to call the right function when required. By using overloading, a single function called ‘Add’ can add integers, floating point values, strings, records etc. When the ‘Add’ function is called, the right function would be called depending on the types of input given to it. This is contrary to parametric polymorphism where such a thing is not possible. Thus, overloading enables the programmer to have a function perform two completely different things based on the type of input passed to it. A function is differentiated from others based on the number of arguments, their order and their data types.

Overloading, as a type of polymorphism is characteristic of object-oriented programming languages, many of which even allow operators to be overloaded in a manner similar to functions which is then called operator overloading. C and C++ provide for such overloading natively. Under operator overloading, the same operator can perform various operations on its arguments. For example, the ‘+’ sign maybe used to add to numbers and at the same time concatenate two arrays of characters (strings).

Post a Comment

Previous Post Next Post