Blog author:
Saurabh Singh | Posted on: 4/8/2012 | Category:
C# Blogs | Views: 1218 | Status:
[Member] |
Points: 75
|
Alert Moderator
This is a very general question difference between new and override keyword ? Everyone knows the answer but still everyone has doubts while explaining : Let me explain you :
New : New hides the base class function. What exactly it means ? : They're two
entirely separate methods ( one in base class another one is in derived class) which happen to have the same name, rather than the
derived method overriding the base method.
e.g : If we make a virtual method in base class and override it using new keyword in child class then while creating object of derived class with reference to base class like :
public class BaseTest
{
public virtual void SomeMethod()
{
Console.WriteLine("Inside Base Class");
}
}
public class NewTest : BaseTest
{
public new void SomeMethod()
{
Console.WriteLine("Inside New Derived Class");
}
}
public class OverrideTest : BaseTest
{
public override void SomeMethod()
{
Console.WriteLine("Inside override Derived Class");
}
}
BaseTest obj = new NewTest();
//It calls the base class method.
obj.SomeMethod();
//Output should be Inside Base Class
Override : Overrides the base class behavior. While implementing a virtual method with override keyword in child class. We override it's own behavior.
BaseTest obj = new OverrideTest();
//It calls the derived class method.
obj.SomeMethod();
//Output should be Inside override Derived Class
Saurabh Singh
http://tech-giant.blogspot.com
Found interesting? Add this to: