Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 14389 |  Welcome, Guest!   Register  Login
 Home > Blogs > C# > What's difference between New and Override keyword ? ...
Saurabh Singh

What's difference between New and Override keyword ?

 Blog author: Saurabh Singh | Posted on: 4/8/2012 | Category: C# Blogs | Views: 1218 | Status: [Member] | Points: 75 | Alert Moderator   
Ads

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:



 More Blogs from Saurabh Singh

     More ...

    Experience:3 year(s)
    Home page:http://www.isaurabh.com
    Member since:Sunday, April 08, 2012
    Level:Starter
    Status: [Member]
    Biography:
    >> Write Response - Respond to this post and get points

    More Blogs

    About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
    General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
    Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 5:37:27 PM