What's the difference between override
and new
?
This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of
the object that the reference refers to is used to decide which method implementation to use. When a method
of a base class is overridden in a derived class, the version in the derived class is used, even if the calling
code didn't "know" that the object was an instance of the derived class. For instance:
public class Base
{
public virtual void SomeMethod()
{
}
}
public class Derived : Base
{
public override void SomeMethod()
{
}
}
...
Base b = new Derived();
b.SomeMethod();
will end up calling Derived.SomeMethod
if that overrides Base.SomeMethod
. Now,
if you use the new
keyword instead of override
, the method in the derived class
doesn't override the method in the base class, it merely hides it. In that case, code like this:
public class Base
{
public virtual void SomeOtherMethod()
{
}
}
public class Derived : Base
{
public new void SomeOtherMethod()
{
}
}
...
Base b = new Derived();
Derived d = new Derived();
b.SomeOtherMethod();
d.SomeOtherMethod();
Will first call Base.SomeOtherMethod
(line 3), then Derived.SomeOtherMethod
(line 4).
They're effectively two entirely separate methods which happen to have the same name, rather than the derived method
overriding the base method.
If you don't specify either new
or overrides
, the resulting output is the same
as if you specified new
, but you'll also get a compiler warning (as you may not be aware that you're
hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to
include the keyword).
That provides the basics of overriding and the difference between new
and override
,
but you should really see a book or tutorial for a more in-depth look at polymorphism.
[Author: Jon Skeet]
Anonymous
March 11, 2004
Another difference:
virtual - good practice
new - well, let's just say you'd better have a really good reason for using it!
:-)Anonymous
March 11, 2004
You forgot the virtual keyword in your first example.Anonymous
March 11, 2004
The Base.SomeMethod method needs to be declared virtual, or you'll get a compiler warning/error about trying to override a non-virtual function.Anonymous
March 12, 2004
Doh! Thanks...Anonymous
March 17, 2004
You might also want to mention what the motivation was for C# to have 'new' and 'override' keywords when no other pre-.NET language (that I'm aware of) does.
Namely, to solve a form of the fragile base class problem. A new version of a base class may be released which happens to introduce a method with the same name as a method in your derived class. Since these two methods have no relationship to eachother, except that the happen to have the same name, you certainly don't want to start overriding the new base method.Anonymous
March 23, 2004
This'll help me for my midterm =). Thanks!Anonymous
April 14, 2004
It should also be mentioned that one of the few legitimate uses of new is to change the return type of a method or property. It can help making a derived class more strongly typed than the base. We use it for that in ASP.NET in a few places (i.e. adapters)Anonymous
April 27, 2004
The comment has been removedAnonymous
July 21, 2004
aaaAnonymous
December 27, 2004
[http://itpeixun.51.net/][http://aissl.51.net/][http://kukuxz003.freewebpage.org/][http://kukuxz001.51.net/][http://kukuxz003.51.net/][http://kukuxz005.51.net/][http://kukuxz002.51.net/][http://kukuxz004.freewebpage.org/][http://kukuxz007.51.net/][http://kukuxz001.freewebpage.org/][http://kukuxz006.51.net/][http://kukuxz002.freewebpage.org/][http://kukuxz004.51.net/][http://kukuxz008.51.net/][http://kukuxz009.51.net/][http://kukuxz005.freewebpage.org/][http://kukuxz006.freewebpage.org/][http://kukuxz007.freewebpage.org/][http://kukuxz009.freewebpage.org/]Anonymous
May 28, 2010
good ex : blog.flair-systems.com/.../c-fastfood-difference-between-override.htmlAnonymous
June 06, 2010
great article.. thanksAnonymous
July 07, 2010
Nice.pls explain fragile base class problem .............Anonymous
July 25, 2010
yeah, a good answer for me to understand what "hide" and "override" means. Thanks :-)Anonymous
October 20, 2010
Good article, helps me a lot. I think both "override" and "new" are something of security mechanism which is used to remind programmer possible spelling mistake in codes.Anonymous
November 28, 2010
public class Para { public virtual void print() { System.Console.WriteLine("Para para para..."); } } public class Manat : Para { public new void print() { Console.WriteLine("Pul pul pul..."); } } public class Dollar : Para { public override void print() { Console.WriteLine("Dollar..."); } } static void Main(string[] args) { Para m = new Manat(); m.print(); Para d=new Dollar(); d.print(); } OUTPUT: Para para para... Dollar...Anonymous
June 14, 2012
Please tell the use case of Hiding and overriding so that I can compare its real world use.Anonymous
August 09, 2012
Indeed explaination with the piece of code. Thanks :)Anonymous
July 28, 2014
i want to delete primary key value that is having identity, when i delete that value , next value should generate last deleted value..