Friday, 29 March 2013

method overriding in c# with real time examples

same method names with same arguments and same return types associated in a class and its subclass. 

Examples:


class BC
{
  public virtual void Display()
  {
     System.Console.WriteLine("BC::Display");
  }
}

class DC : BC
{
  public override void Display()
  {
     System.Console.WriteLine("DC::Display");
  }
}

class TC : DC
{
  public override void Display()
  {
     System.Console.WriteLine("TC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;
     b = new BC();
     b.Display();  

     b = new DC();
     b.Display();  

     b = new TC();
     b.Display();  
  }
}
Output

BC::Display
DC::Display
TC::Display

No comments:

Post a Comment