Monday, 9 July 2012

Polymorphism On OOPS

Polymorphism :
                  Polymorphism exists in OOPS. Polymorphism is associated with a class , method and objects . By polymorphism we mean that the method or class that we talk about exhibits different property at different situations.Polymorphism comes into picture because of the following ,
                  1) Inheritance
                  2) Overriding
                  3) Dynamic binding
                  4) Overloading

A simple example :
          class output{
                 public :
                 print(string s) {cout<<s;}
                 print(int s){cout<<s;}
           }
            int main()
            {
                output o; o.print("my score is ") ; o.print( 0 );
             }
        
              This is called as overloading a function and we can discuss about this later. The thing to note is even though the same function name is used the function called are different.

Why Polymorphism :
            Most people wont find the need to go for the overloading concepts . It wont be difficult to have a different name for different type of execution. But the key idea to go for polymorphism is to abstract the business logic from the users. For example : you know that decimal values are stored in C . But you never know what how they are stored. The reason is that it is not important.

Inheritance :
             Inheritance is deriving the base class methods and the attributes by the derived class.
                Class vessel
                 {
                              int litres;
                              void hold_water(int liters){volume=litres;}
                               bool will_break()=0;
                 }
                 Class Plastic_bottle : public vessel
                  {
                              bool will_break(){return false;}
                  }
                 Classic Glass_bottle : public vessel
                 {
                             bool will_break(){return true;}
                  }
                  int main()
                  {
                         Class * bottle = new Plastic_bottle();
                         cout<<bottle.will_break();   // will return 0
                         bottle = new Glass_bottle();
                         cout<<bottle.will_break();   // will return 1
                   }
            Polymorphism is linked with inheritance because a vessel can be made to contain water no matter what type is it in, either a Glass_bottle or a Plastic_bottle. But it might break depending on the material from which it was built from.


Overriding and Late Binding:
             Overriding is used in Inheritance when the derived class needs to implement certain methods its own way which are already defined in the parent class. So it overrides the method  in the parent class with the same name.

               Class Rented_vechile
                {
                      virtual void collect_money(){ Get money for traveled distance;}
                }
                Class Share_auto: Rented_vechile
                {
                       void collect_money(){Get money from each individual for his travel;}
                }
                int main()
                {
                     Rented_vechile *rv=Share_auto();
                     rv->collect_money();
                }
             The above code has called for the method and there is no need to explicitly mention that it to the derived class. This can be quoted as an example for late binding as well. In late binding the compiler doesn't know to which object's method is getting called until run-time, bcoz both the base class and derived class have the same method.

 Overloading:
          The methods which you invoke from certain objects execute differently depending on the parameters passed on to it. This is method level polymorphism.
            Operator overloading in C++ is a special kind of polymorphism in C++ because you cannot replace the original functionality of the operators but you can add additional functionality to them.
           For example :  The operator '+' will execute for addition for numbers. We can add additional functionality to this by making it concatenate two strings.

No comments:

Post a Comment