Tuesday, December 15, 2015

Polymorphism


   >>> Polymorphism means the ability to take more than one form.

   >>> One name many forms.
   
   >>> One interface multiple methods/ways.

   >>> An operation may exhibit different behaviours in different instances. The behaviour depends on the data types used in the operation.
   

    >>> Example:
                          
                           void add (int a, int b)
                           void add (int a, int b, int c)
                           int add (double a, double b)
    
                           cout << add(5,2)
                           cout << add(5,2,3)
                           ans = add(2,3,4,5)
                           cout <<ans
                                                   

   >>> Polymorphism is the ability of behaviour to vary based on the conditions in which the behaviour is invoked that is two or more methods as well as operators ( such as +, -, *  among others) can fit to many different conditions.

   >>> For Example, if a Dog is commanded to speak() this may elicit a bark; if a pig is commanded to speak() this may elicit on oink. This is expected because Pig has a particular implementation inside the speak() method. The same happens to class Dog. Considering both of them inherit speak() from animal this is an example of Overriding Polymorphism.

   >>> Another good example is about Overloading Polymorphism a very common one considering operators like "+". Once defined an operator used to add numbers given a class number and also given two other classes that inherits from number such as Integer and Double.

   >>> Any programmer expect to add two distances of Double or two instances of Integer in just the same way and more than this: Any programmer expects the same behaviour to any number. In this case the programmer must overload the concatenation operator "+", by making it able to operate with both Double and Integer instances.

   >>> The way it is done varies a little bit from one language to another and must be studied in more details according to the programmer interest.

   >>> Most of the OOP languages support small differences in method signatures as polymorphism. It's very useful once it improves code readability to enable implicit conversions to the correct handling method when apply add() method to integers  like in add(1,2) or to strings like in add("foo","bar") since the definitions of these signatures are available. In many OOP languages such methods signature would be respectively very similar to add(int a int b) and add(String a String b). This is an example of Parametric Polymorphism. The returned type and used modifiers of course depend on the programmer interest and intentions.

   >>> Polymorphism is extensively used in implementing Inheritance.

   >>> There are basically two types of Polymorphism. Compile time (also knows as early binding) and Run time (also knows as late Binding, Dynamic Binding) Polymorphism.

   >>> In compile time polymorphism object know about itself at compile time.

   >>> Overloading is a compile time polymorphism

   >>> In Overloading method should have same name with different arguments.

   >>> Simple example of overloading is (as given example) if you have scenario in which you want do Add of two or three number what ever user will pass. So you can create two methods with same name "Add" and assign 2 and 3 arguments into it.

   >>> In Run Time Polymorphism, Object does not know about itself at compile time it assigns all the properties and methods at runtime.

   >>> Dynamic Binding means that code associated with a given procedure call in not known until the time of the call at run - time.

   >>> Overloading or inheritance -based polymorphism are kind of polymorphism.



   >>> Simple and very common example I am talking about above figure a class shape which is inherited to Triangle, Square and Circle classes.

   >>> Shape class has a method name as "Draw" which will definitely inherited to all inherited class.

   >>> Now, if you declare a variable of shape class and initialize it with any of the inherited class it will call the method of inherited class.


1 comment: