Sunday, February 26, 2012

Override and New keywords

Abstract or virtual members(method/events/indexer) of base class can be redefined in derived class, this is called overriding. An Abstract member is implicitly Virtual.

The method you are overriding must have same signature with overridden method. An overridden method of base class also can be overridden in derived class, because override shows that method exist in some upper class in inheritance hierarchy.

You can't use static, new or virtual to modify method with override.

Reasons
-
With Static - Static makes a method common for all instances, static method is not an instance method so it can't take part in inheritance.

With New - You can have same signature method in derived class compare to base class method with keyword Override or New. Override makes sense that you are redefining a base class method in derived class. New makes sense that it is a fresh new method not having any connection with base class method, it hides existence of base class method in derive class it called Hiding (or Shadowing in VB.Net), Use of New is not recommended because it breaks rules of OOP.

With Virtual - Because we can override a method that is already Virtual.
Some examples to understand it -

Below examples showing Runtime/Dynamic polymorphism.

How runtime calls methods in these scenarios?

First CLR checks runtime type of object, then starts searching last derived form of called method from variable type(compile time type) class to the runtime type class, and calls the last derived form of the called method in between this path.  

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Sample E
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();    
  }
}

Sample F
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
{

}

class Demo
{
  public static void Main()
  {
     BC b;

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

Sample G
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 new void Display()
  {
     System.Console.WriteLine("TC::Display");
  }
}

class Demo
{
  public static void Main()
  {
     BC b;

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


Output :
A
BC::Display

B
BC::Display
BC::Display

C
BC::Display
BC::Display
BC::Display

D
BC::Display
DC::Display

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

F
DC::Display

G
DC::Display

No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...