Navigation

Monday 17 June 2013

Generics Support Inheritance in C#

//Why Generics is Needed?

using System;

//Generics Support Inhertance,where derived class can decide the type

//T^his is Parametric Pilymorphism

class person<T>
{
     public T pid;

    public string PName;

   public person(T x, string y) //Generic P.C
   {
    pid = x;
    PName = y;
    }
}

//first client user person for int
class employee:person<int>
    {
       public employee(int x, string y):base(x,y)
        {
        }

       public void show()
        {
            Console.WriteLine(this.pid + " " + this.PName);
         }
      }

//second client use person as string
  class hr : person<string>
    {
        public hr(string x, string y):base(x,y)
        {

        }

        public void show()
        {
           Console.WriteLine(this.pid + " " + this.PName);
         }

   }

class abc
{
     public static void Main()
      {
 
       //first client call this class
        employee e = new employee(101, "java");
       e.show();

      //second client call this class
        hr h = new hr("sandeep", "OOPS");
        h.show();

     }

}

No comments:

Post a Comment