Friday, October 28, 2016

                                            Polymorphism

In object-oriented programming, Polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. In other words, It is the ability to redefine methods for derived classes. Polymorphism is considered to be a requirement of any true Object-Oriented Programming Language (OOPL). There can we several types of Polymorphism.

What are the different types of Polymorphism ?

  • Ad hoc polymorphism: When the function denotes different heterogeneous implementations depending on a limited range of individually specified types and combinations. Ad hoc polymorphism is supported in many languages using function overloading.
  • Parametric polymorphism: When code is written without mention of any specific time and thus can be used transparently with any number of new types. In object-oriented programming community, this is often known as generics or generic programming. In functional programming community, this is often simply referred to as polymorphism.
  • Sub typing :This is also called subtype polymorphism or inclusion polymorphism. When a name denotes instances of many different classes related by some common superclass. In the object-oriented programming community, this is often simply referred to as polymorphism.
The interaction between parametric polymorphism and sub typing that leads to the concepts of variance  and bounded quantification. 

When is Polymorphism useful ?

The usefulness of Polymorphism depends on the programmer's design and logic. If the programmer  wants to bind your implementation at runtime then Subtype polymorphism will be useful. If you want to invoke different implementation at runtime based on the input arguments then ad-hoc polymorphism will help. If the programmer wants to write a code with a family of types, the code can ignore type specific details and  just interact with the base type of family. Even though the code thinks it is using an object's class could actually be the base class or any one of its subclasses. This makes it easier the programmer to write the code and the readers to understand.Overall, polymorphism provides a powerful feature in binding the  implementation at runtime and provides design extensibility.
_________________________________________________________________________________

References



_________________________________________________________________________________

1 comment:

  1. The best example of Polymorphism in life may be humans. We have different behaviors in different situations and in front of different people. A man behaves differently with his father, when he is with his friend he has different behavior, when he is with his wife he has different behavior and so on.

    ReplyDelete