जगदीश खोलिया: Concept of polymorphism

Wednesday, May 16, 2012

Concept of polymorphism


What is Polymorphism?

  • Polymorphism means many forms.
  1. It has the ability for classes to provide different implementations of methods that are called through the same name.
  2. It allows you to invoke methods of derived class through base class reference during runtime.
    • Polymorphism is one of the primary characteristics (concept) of object-oriented programming.
    • Poly means many and morph means form. Thus, polymorphism refers to being able to use many forms of a type without regard to the details.
    • Polymorphism is the characteristic of being able to assign a different meaning specifically, to allow an entity such as a variable, a function, or an object to have more than one form.
    • Polymorphism is the ability to process objects differently depending on their data types.
    • Polymorphism is the ability to redefine methods for derived classes.

    Types of Polymorphism

    • Compile time Polymorphism
    • Run time Polymorphism

    Compile time Polymorphism

    • Compile time Polymorphism also known as method overloading
    • Method overloading means having two or more methods with the same name but with different signatures
    • Example of Compile time polymorphism

     Run time Polymorphism

    • Run time Polymorphism also known as method overriding
    • Method overriding means having two or more methods with the same name , same signature but with different implementation

    Example of Run time Polymorphism


    You can find source code at the bottom of this article.


    What is polymorphism? Poly means many. So how can we take this definition in .NET context. Well we can apply this to class's ability to share the same methods (actions) but implement them differently.
    Suppose we have a method in a class to add numbers,
    1 public class calculation
    2 {
    3 public int add(int x, int y)
    4 {
    5   return x+y;
    6 }
    7 }

    So to perform addition of three numbers, we need similar add method but with different parameter
    01 public class calculation
    02 {
    03 public int add(int x, int y)
    04 {
    05   return x+y;
    06 }
    07  public int add(int x, int y,int z)
    08 {
    09   return x+y+z;
    10 }
    11 }

    So we can that class is sharing the same methods (actions) but implement them differently.
    Now this is an example when we are sharing method name and implementing them differently, let's take a scenario where implementation is in some derived class.
    For instance, say we create a class called Shape and this class has a method called .Area () which calculates area. Then we create two subclasses, using inheritance, of this Shape class. One called Square, the other called Circle. Now obviously a square and circle are two entirely different shapes, yet both classes have the .Area() method. When the Square.Area() method is called it will calculate area of  a square. When the Circle.Area() method is called, it will calculate area of a circle. So both classes can use the same methods but implement them differently.

    Now let's dive little deeper and understand what we discussed above in more technical terms.

    1) Static or Compile time Polymorphism


    Which method is to be called is decided at compile-time only. Method Overloading is an example of this. Method overloading is a concept where we use the same method name many times in the same class, but different parameters. Depending on the parameters we pass, it is decided at compile-time only. The same method name with the same parameters is an error and it is a case of duplication of methods which c# does not permit. In Static Polymorphism decision is taken at compile time.

    public Class StaticDemo
    {
      public void display(int x)
     {
      Console.WriteLine("Area of a Square:"+x*x);
     }
      public void display(int x, int y)
     {
      Console.WriteLine("Area of a Square:"+x*y);
     }
     public static void main(String args[])
     {
      StaticDemo spd=new StaticDemo();
      Spd.display(5);
      Spd.display(10,3);
     }
    }


    2) Dynamic or Runtime Polymorphism.


    Run time Polymorphism also known as method overriding. In this Mechanism by which a call to an overridden function is resolved at a Run-Time (not at Compile-time) if a base Class contains a method that is overridden. Method overriding means having two or more methods with the same name, same signature but with different implementation. In this process, an overridden method is called through the reference variable of a superclass, the determination of the method to be called is based on the object being referred to by reference variable.
    01 Class BaseClass
    02 {
    03   Public void show ()
    04  
    05   Console.WriteLine("From base class show method");
    06   }
    07 }
    08 Public Class DynamicDemo : BaseClass
    09 {
    10   Public void show()
    11   {
    12   Console.WriteLine("From Derived Class show method");
    13   }
    14   Public static void main(String args[])
    15   {
    16   DynamicDemo dpd=new DynamicDemo ();
    17   Dpd.show();
    18    
    19   }
    20 }
    Here memory allocation will be at the run time for that particular method.

    What is Virtual Function: They implement the concept of polymorphism are the same as in C#, except that you use the override keyword with the virtual function implementation in the child class. The parent class uses the same virtual keyword. Every class that overrides the virtual method will use
    the override keyword.
    Why to use them:

    1)It is not compulsory to mark the derived/child class function with Override KeyWord while base/parent class contains a virtual method
    2)Virtual methods allow subclasses to provide their own implementation of that method using the override keyword

    3)Virtual methods can't be declared as private.

    4)You are not required to declare a method as virtual. But, if you don't, and you derive from the class, and your derived class has a method by the same name and signature, you'll get a warning that you are hiding a parent's method

    5)A virtual property or method has an implementation in the base class, and can be overriden in the derived classes.

    6)
    We will get a warning if we won't use Virtual/New keyword.

    7)Instead of Virtual we can use New Keyword
    01 class A
    02 {
    03 public void M()
    04 {
    05 Console.WriteLine("A.M() being called");
    06 }
    07
    08 public virtual void N()
    09 {
    10 Console.WriteLine("A.N() being called");
    11 }
    12 }
    13
    14 class B : A
    15 {
    16 public new void M()
    17 {
    18 Console.WriteLine("B.M() being called");
    19 }
    20
    21 public override void N()
    22 {
    23 Console.WriteLine("B.N() being called");
    24 }
    25 }



    say

    A a = new B();

    a.M();
    a.N();


    The results would be

    A.M() being called
    B.N() being called

    Override Keyword: method overriding is modifying or replacing the implementation of the parent class with a new one. Parent classes with virtual or abstract members allow derived classes to override them.
    01 class Shape
    02 {
    03   public virtual void Draw()
    04   {
    05   Console.WriteLine("Shape.Draw")  ;
    06   }
    07 }
    08
    09 class Rectangle : Shape
    10
    11 {
    12   public override void Draw()
    13   {
    14   Console.WriteLine("Rectangle.Draw");
    15   }
    16 }

    No comments: