473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Differnce between virtual/override and new

hi
what is the difference between virtual / override and new? until now i only
knew the virtual / override thing.
thanx for hint.
Jazper

//--- new ----------------------------------------
public class MyBaseA
{
public void Invoke() {}
}
public class MyDerivedA : MyBaseA
{
new public void Invoke() {}
}

//---virtual / override -----------------------------
public class MyBaseB
{
public virtual void Invoke() {}
}
public class MyDerivedB : MyBaseB
{
public override void Invoke() {}
}

Nov 16 '05 #1
4 2197
Jazper Manto <ej*****@hotmail.com> wrote:
what is the difference between virtual / override and new? until now i only
knew the virtual / override thing.


See http://www.pobox.com/~skeet/csharp/faq/#override.new

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Jazper Manto wrote:
what is the difference between virtual / override
and new? until now i only knew the virtual / override
thing.
As you say you already know the use of virtual/override, you know that it's
a construction that facilitates polymorhism.

The "new" construction isn't a "polymorph" thingy, as it only can be used to
*hide* a method implementation from the superclass, but *only* when the type
of the variable is of the subclass type (hence no polymorphism).

Based on your examples, I'll show the difference:

MyBaseA ma1 = new MyDerivedA();
MyDerivedA ma2 = new MyDerivedA();

ma1.Invoke(); // runs the method in the superclass
ma2.Invoke(); // runs the method in the subclass

MyBaseB mb1 = new MyDerivedB();
MyDerivedB mb2 = new MyDerivedB();

mb1.Invoke(); // runs the method in the subclass
mb2.Invoke(); // runs the method in the subclass

As you see, the virtual/override construction looks at what type the actual
instance is, while new only looks at what type the *variable* is.
Hope this made it clearer.

// Bjorn A

P.S. It's more common to put the "new" keyword after the visibility keyword,
such as:

public new void Invoke() {}
//--- new ----------------------------------------
public class MyBaseA
{
public void Invoke() {}
}
public class MyDerivedA : MyBaseA
{
new public void Invoke() {}
}

//---virtual / override -----------------------------
public class MyBaseB
{
public virtual void Invoke() {}
}
public class MyDerivedB : MyBaseB
{
public override void Invoke() {}
}

Nov 16 '05 #3
hi Bjorn
Hope this made it clearer.

Perfect explanation. excactly what i was looking for! thank you.
PS: thanx for hint in your PS! :-)

regards, Jazper
Nov 16 '05 #4
hi Jon
See http://www.pobox.com/~skeet/csharp/faq/#override.new

thanx for FAQ page!
regards, jazper
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
4
by: Rafael Veronezi | last post by:
I have some questions about override in inheritance, and virtual members. I know that you can you override a method by two ways in C#, one, is overriding with the new keyword, like: public new...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
8
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
4
by: Jazper Manto | last post by:
hi what is the difference between virtual / override and new? until now i only knew the virtual / override thing. thanx for hint. Jazper //--- new ----------------------------------------...
5
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two...
15
by: John Salerno | last post by:
Hi all. I have a question about virtual and override methods. Please forgive the elementary nature! First off, let me quote Programming in the Key of C#: "Any virtual method overridden with...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
8
by: Karsten Schramm | last post by:
Hi, when I run the following code: using System; namespace ConsoleApplication22 { class Program {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.