By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,352 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,352 IT Pros & Developers. It's quick & easy.

Differnce between virtual/override and new

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
hi Jon
See http://www.pobox.com/~skeet/csharp/faq/#override.new

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

This discussion thread is closed

Replies have been disabled for this discussion.