Friday, 7 February 2014

Polymorphism in C#

                                                                                                                                                Previous....

                                                                                                                                                     Next.....


The word polymorphism means having more then one forms. 
Polymorphism can be static or dynamic. In static polymorphism it is determined at the compile time. In dynamic polymorphism , it is decided at run-time.

Static Polymorphism/Function Overloading

It is an compile time polymorphism  because compiler already knows what type object it is linking to and waht type of method going to call. So linking a method at compiler time also called as early binding.
"In this Code method name same but its parameters are different  so it is called operate overloading."
using System;
namespace Polymorphism
{
   class calculation 
   {
      public int add(int a, int b)
      {
         return(a+b);
      }

      public int add(int a, int b,int c)

      {
         return(a+b+c);

      }

     }
Dynamic Polymorphism
It is an Run time polymorphism  because it is link at run time. So linking a method at run time also called as late binding.
"In this Code method name same and its parameters are also same  so it is called operate overriding."

In base class the method is declare "virtual" and in the derived class we override the same method. the virtual keyword indicate the method can be overridden in any derived class.
During run time, method overriding can be achieve by using inheritance principle and using "virtual" and "override" keyword.    
using System;
namespace employee
{
   public string firstname = "FN";
   public string secondname = "SN";

      public virtual void printfullname()
   console.writeline(firstname + " " + lastname);         }}

   public class parttime employee : employee
   {
      public override void printfullname()
      console.writeline("firstname + " " lastname + " parttime");  }}

      public class fulltimeemployee : employee
      {
         public override void printfullname()
        {
      console.writeline("firstname + " " lastname + " fullname");  }}
public class temporaryemployee : employee 
      { public override printfullname()
      { 
        console.writeline("firstname + " " lastname + " temporary ");  }}

         public class programme
      { public static void main()
   employee[] emp = new employee[4];
   emp [0] = new employee();
   emp [1] = new parttimeemployee();
   emp [2] = new fulltimeemployee();
   emp [3] = new tomporarytime employee();
   
     foreach(employee e in emp)

       {
         e.printfullname();
         
      }
   }
}
When the above code is compiled and executed, it produces the following result:
FN SN ,FN SN parttime

FN SN fulltime,FN SN temporary 
                                                                                                                                                                                              Previous....
                                                                                                                                                     Next.....

No comments:

Post a Comment

Paging in html table using javascript

I demonstrated how to use HTML tables on the client for a very simple client-side paging solution. I have heard from several people who p...