Hello, yes another beginner question that I'm sure is obvious to many here
:-)
My book is so bad. Really. It uses the exact same example of code for using
the new kw and for using virtual(in the base class) then override(in the
derived class), but fails to compare and contrast the two...
I've read this (short snippet)three times and by the looks of it they both
do exactly the same thing- allow you to derive from the base and save
writing some code in the derived class. It seems they both say "yes, use
this derived method, slightly modified, and don't use the original one in
the base class"
I know I'm missing something big here.
Any compare/contrast of using new vs using virtual and override much
appreciated.
If it's too long, maybe a reference to a good white paper or something(?)
Thanks!
Jeff 11 19458
Hi,
The difference between the override keyword and new keyword is that the former does method overriding and the later does method hiding.
Check out the folllowing links for more information...
<http://msdn.microsoft. com/library/default.asp?url =/library/en-us/csref/html/vcwlkversioning tutorial.asp> http://www.akadia.com/services/dotnet_polymorphism.html
Hope this helps...
--
Regards,
Madhu
Microsoft C# MVP | MCSD.NET
"z_learning_tes ter" wrote: Hello, yes another beginner question that I'm sure is obvious to many here :-)
My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the base class) then override(in the derived class), but fails to compare and contrast the two...
I've read this (short snippet)three times and by the looks of it they both do exactly the same thing- allow you to derive from the base and save writing some code in the derived class. It seems they both say "yes, use this derived method, slightly modified, and don't use the original one in the base class"
I know I'm missing something big here. Any compare/contrast of using new vs using virtual and override much appreciated. If it's too long, maybe a reference to a good white paper or something(?) Thanks!
Jeff
OK, the MSDN example looked great until this line of code: "MyBase mB =
(MyBase) mD;"
Never seen anything like it. Is this a cast?
Does that mean mD is cast to a MyBase type?
How about if that line was missing? would the output be the following
instead?
MyBase-Meth1
MyDerived-Meth2
MyDerived-Meth3
This is what I was (almost) expecting, until that crazy unexplained line
above.
Thanks!
Jeff
--------------------------------------------
public class MyBase
{
public virtual string Meth1()
{
return "MyBase-Meth1";
}
public virtual string Meth2()
{
return "MyBase-Meth2";
}
public virtual string Meth3()
{
return "MyBase-Meth3";
}
}
class MyDerived : MyBase
{
// Overrides the virtual method Meth1 using the override keyword:
public override string Meth1()
{
return "MyDerived-Meth1";
}
// Explicitly hide the virtual method Meth2 using the new
// keyword:
public new string Meth2()
{
return "MyDerived-Meth2";
}
// Because no keyword is specified in the following declaration
// a warning will be issued to alert the programmer that
// the method hides the inherited member MyBase.Meth3():
public string Meth3()
{
return "MyDerived-Meth3";
}
public static void Main()
{
MyDerived mD = new MyDerived();
MyBase mB = (MyBase) mD;
System.Console. WriteLine(mB.Me th1());
System.Console. WriteLine(mB.Me th2());
System.Console. WriteLine(mB.Me th3());
}
}
Output:
MyDerived-Meth1
MyBase-Meth2
MyBase-Meth3
"Madhu[C#-MVP]" <Ma*******@disc ussions.microso ft.com> wrote in message
news:9E******** *************** ***********@mic rosoft.com... Hi,
The difference between the override keyword and new keyword is that the
former does method overriding and the later does method hiding. Check out the folllowing links for more information...
<http://msdn.microsoft.com/library/de...-us/csref/html
/vcwlkversioning tutorial.asp> http://www.akadia.com/services/dotnet_polymorphism.html
Hope this helps...
-- Regards, Madhu
Microsoft C# MVP | MCSD.NET
"z_learning_tes ter" wrote:
Hello, yes another beginner question that I'm sure is obvious to many
here :-)
My book is so bad. Really. It uses the exact same example of code for
using the new kw and for using virtual(in the base class) then override(in the derived class), but fails to compare and contrast the two...
I've read this (short snippet)three times and by the looks of it they
both do exactly the same thing- allow you to derive from the base and save writing some code in the derived class. It seems they both say "yes, use this derived method, slightly modified, and don't use the original one
in the base class"
I know I'm missing something big here. Any compare/contrast of using new vs using virtual and override much appreciated. If it's too long, maybe a reference to a good white paper or
something(?) Thanks!
Jeff
OK, so I went throught the MSDN examples today and they say:
Override is used in the derived class to say "use this one instead of the
one in the base class" I am overriding the one in the base class.
New also says "use this one instead of the one in the base class" I am
hiding the one in the base class.
I still fail to see the difference if you override the base class or if you
hide it, you still get the same results: the one on the derived class is
used instead.
The only difference I could find is the exception below, where new must be
used on MyBaseC instead of virtual and override b/c you cannot use virtual
with static.
So my question is- is there anything that new does that override doesn't or
vice-verse?
Aside from being usable with static? What's the difference between hiding
and overriding, or do they both just say "ignore the base, use the derived"?
This is by far the most difficult concept I've found in C#.
All examples look the same...
Again, any help *much* appreciated.
Thanks!
Jeff
public class MyBaseC
{
public static int x = 55;
public static int y = 22;
}
public class MyDerivedC : MyBaseC
{
new public static int x = 100; // Name hiding
public static void Main()
{
// Display the overlapping value of x:
Console.WriteLi ne(x);
// Access the hidden value of x:
Console.WriteLi ne(MyBaseC.x);
// Display the unhidden member y:
Console.WriteLi ne(y);
}
}
Output
100
55
22"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... z_learning_test er <so*****@micros oft.com> wrote: Any compare/contrast of using new vs using virtual and override much appreciated.
See http://www.pobox.com/~skeet/csharp/faq/#override.new
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
z_learning_test er <so*****@micros oft.com> wrote: OK, so I went throught the MSDN examples today and they say:
Override is used in the derived class to say "use this one instead of the one in the base class" I am overriding the one in the base class. New also says "use this one instead of the one in the base class" I am hiding the one in the base class.
I still fail to see the difference if you override the base class or if you hide it, you still get the same results: the one on the derived class is used instead.
The difference is what the *base* class will use if it calls the
method. The link I provided gave an example of the difference, saying
which would be called when.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... z_learning_test er <so*****@micros oft.com> wrote: OK, so I went throught the MSDN examples today and they say:
Override is used in the derived class to say "use this one instead of
the one in the base class" I am overriding the one in the base class. New also says "use this one instead of the one in the base class" I am hiding the one in the base class.
I still fail to see the difference if you override the base class or if
you hide it, you still get the same results: the one on the derived class is used instead. The difference is what the *base* class will use if it calls the method. The link I provided gave an example of the difference, saying which would be called when.
Yep, that seems the part I was really missing, that seems the key!
These derived classes are not to be used on their own.
I think the WHOLE point is to put all these methods back into the base class
type by a cast, or into a base class type array, etc, then use that instance
of the base class type.
Their choice of verbage however, though I'm sure is very standard,
is also very unintuitive in regards to hiding because all the lingo seems
reversed.
That caught me up for so long...
After all, the base method that is "hidden"(by the derived class- using
"new") is the one that shows up!
The one that is "hidden" by the derived class using no kw at all, also shows
up.
Dang that's confusing.
Now override is the only one that seems to make sense,
if it overrides the base then it well, overrides it, and is used instead of
the base(great! :-)
// versioning.cs
// CS0114 expected
public class MyBase
{
public virtual string Meth1() //This one is "overridden " so it
doesnt show- OK :-)
{
return "MyBase-Meth1";
}
public virtual string Meth2() //This one is "hidden", so it shows!
{
return "MyBase-Meth2";
}
public virtual string Meth3() //This one is "hidden", so it shows!
{
return "MyBase-Meth3";
}
}
class MyDerived : MyBase
{ public override string Meth1() //This one "overrides" the base-
cool.
{
return "MyDerived-Meth1";
}
public new string Meth2() //Usually in life, the 'newer' thing
is used... {
return "MyDerived-Meth2" }
public string Meth3() //This one "hides" the base, so on
output, it is itself- hidden. {
return "MyDerived-Meth3";
}
public static void Main()
{
MyDerived mD = new MyDerived();
MyBase mB = (MyBase) mD;
System.Console. WriteLine(mB.Me th1());
System.Console. WriteLine(mB.Me th2());
System.Console. WriteLine(mB.Me th3());
}
}Output
MyDerived-Meth1
MyBase-Meth2
MyBase-Meth3 -- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Sorry, I used one of the other folk's examples from MSDN in my last
response.
Your example is definitely one of the cleanest yet and I think actually
answers my questions('hidi ng' terminology aside ;-)
public class Base
{
public virtual void SomeOtherMethod ()
{
}
}
public class Derived : Base
{
public new void SomeOtherMethod ()
{
}
}
....
Base b = new Derived();
Derived d = new Derived();
b.SomeOtherMeth od(); //calls base.SomeOtherM ethod()
d.SomeOtherMeth od(); //calls derived.SomeOth erMethod()
Just one last question- did you need to use 'virtual' here?
Would it make a difference without it?
Thanks again,
Jeff
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... z_learning_test er <so*****@micros oft.com> wrote: OK, so I went throught the MSDN examples today and they say:
Override is used in the derived class to say "use this one instead of
the one in the base class" I am overriding the one in the base class. New also says "use this one instead of the one in the base class" I am hiding the one in the base class.
I still fail to see the difference if you override the base class or if
you hide it, you still get the same results: the one on the derived class is used instead.
The difference is what the *base* class will use if it calls the method. The link I provided gave an example of the difference, saying which would be called when.
-- Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
z_learning_test er <so*****@micros oft.com> wrote: Just one last question- did you need to use 'virtual' here? Would it make a difference without it?
You can only override virtual methods - without the virtual modifier,
the override version wouldn't compile at all.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ed |
last post by:
Greetings!
I'm trying to implement own collection BaseCollection
(inherited from NameObjectCollectionBase class), but my
attempt to override NameObjectCollectionBase virtual method
public virtual new System.Collections.IEnumerator
GetEnumerator ( )
fails with compiller error:
"'bla.bla.bla.BaseCollection.GetEnumerator()' : cannot
override inherited
member 'System.Collections.Specialized.NameObjectCollection
|
by: Raymond Lewallen |
last post by:
I read this on this website page
http://www.vbip.com/books/1861004915/chapter_4915_06.asp:
Unlike many object-oriented languages, all methods in VB.NET are virtual.
Now in BOL, Under Perforamce Tips and Tricks in .NET Applications, A .Net
Developer Platform White Paper, at the very bottom, it says:
The JIT cannot inline virtual methods, so you lose a potential optimization
if you get rid of non-virtual methods.
|
by: gouqizi.lvcha |
last post by:
Hi, all:
I have 3 class X, Y, Z
class Y is a subclass of class X;
class Z is a subclass of class Y;
i.e.
class Y : public class X
class Z : public class Y
|
by: Jon Davis |
last post by:
If I have a class with a virtual method, and a child class that overrides
the virtual method, and then I create an instance of the child class AS A
base class...
BaseClass bc = new ChildClass();
.... and then call the virtual method, why is it that the base class's method
is called instead of the overridden method? How do I fix this if I don't
know at runtime what the child class is? I'm using
Activator.CreateInstance() to load the...
|
by: Adrian Herscu |
last post by:
Hi all,
In which circumstances it is appropriate to declare methods as non-virtual?
Thanx,
Adrian.
| |
by: Hiroyuki Tanaka |
last post by:
Hi All,
I am trying to develop an application for a touch screen using buttons
for the numeric pad with Completion ComboBoxes.
At the moment I am having a problem sending the button presses to my
Completion ComboBox using sendkey.wait. From the keyboard (that will
not exist for my final application) I can enter text into my
Completion and the selection completes as expected.
|
by: William Leary |
last post by:
I'm creating a strongly-typed collection class, and am deriving from CollectionBase. I note in the docs that RemoveAt() is virtual, yet I am not able to override it. For the life of me, I cannot understand why not. (Note that this is not a question of whether I should...I understand the OnRemove event will serve my purpose.) Based on the docs, I believe I should be able to, yet when I compile with a method that overrides RemoveAt, I get an error...
|
by: Phill |
last post by:
I read in one of Jesse Liberty's books that:
"Because ToString() is a virtual method in the base class Object, it
is guaranteed to be available in every derived class." (P.179
Programming C#)
My question is, wouldn't the ToString() method be guaranteed to be
available to derived classes even if it had not been declared as
virtual?
|
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 'override' remains a virtual method for further
descendent classes."
Now here's my question: Let's say you have base class A, and
subclasses B and C. Class A contains a virtual method, and B contains
an override method. If C didn't have an...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |