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 override, does that mean it
inherits the virtual method from A? Is that what the above quote is
saying?
If so, here's another quote: "If necessary, methods or properties can
make calls to overridden members in the base class by prefacing the
method or property name with the keyword 'base.'"
In other words, if base class A contained public virtual int Method(),
and B contained public override int Method(), then for C, if I want to
make use of the method in class A, do I have to write it as:
base.Method()? If so, why would I need to do that if C already
inherits all public methods from A? Couldn't I just use Method()
without the base prefix?
The example in the book contains a base class and several subclasses.
All but one subclass overrides a virtual method in the base class, and
the other subclass (the one that has no override) uses this
'base.Method()' code to refer to the method in the base class. It
seems unnecessary, if this subclass has inherited the method already.
Thanks! 15 2494
> 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 override, does that mean it inherits the virtual method from A? Is that what the above quote is saying?
Since C and B are both derived from A, they will both inherit the methods of
A, yes(one of which B overrides). However, that B overrode one of A's
methods will have no bearing on C.
What that quote is saying is that if you had a set of classes where C
derived from B and B derived from A, when you wrote the override method in B
it would still be a virtual method in C(and thereby available to be
overridden).
If so, here's another quote: "If necessary, methods or properties can make calls to overridden members in the base class by prefacing the method or property name with the keyword 'base.'"
In other words, if base class A contained public virtual int Method(), and B contained public override int Method(), then for C, if I want to make use of the method in class A, do I have to write it as: base.Method()? If so, why would I need to do that if C already inherits all public methods from A? Couldn't I just use Method() without the base prefix?
Yes, you could, in C, but only if you don't mind if further descendents
override the method. Calling base.Method() is basically saying "Hey, I'm
calling A::Method() precisely" whereas just calling Method() is saying "I'm
calling the most recent override of the method Method()"
The example in the book contains a base class and several subclasses. All but one subclass overrides a virtual method in the base class, and the other subclass (the one that has no override) uses this 'base.Method()' code to refer to the method in the base class. It seems unnecessary, if this subclass has inherited the method already.
I would have to see the code to really help. I'm sorry but I don't quite
undersatnd waht you mean here.
Daniel O'Connell [C# MVP] wrote: If so, here's another quote: "If necessary, methods or properties can make calls to overridden members in the base class by prefacing the method or property name with the keyword 'base.'"
In other words, if base class A contained public virtual int Method(), and B contained public override int Method(), then for C, if I want to make use of the method in class A, do I have to write it as: base.Method()? If so, why would I need to do that if C already inherits all public methods from A? Couldn't I just use Method() without the base prefix?
Yes, you could, in C, but only if you don't mind if further descendents override the method. Calling base.Method() is basically saying "Hey, I'm calling A::Method() precisely" whereas just calling Method() is saying "I'm calling the most recent override of the method Method()"
So if I were to use "Method()" in class C, it would be calling the
override method from class B instead of the original method from A?
>> Yes, you could, in C, but only if you don't mind if further descendents override the method. Calling base.Method() is basically saying "Hey, I'm calling A::Method() precisely" whereas just calling Method() is saying "I'm calling the most recent override of the method Method()"
So if I were to use "Method()" in class C, it would be calling the override method from class B instead of the original method from A?
No, atleast not if I understand your set of classes anyway, it'd be calling
the method in A. If you derive two classes from a class, each of those two
classes are entirely independent of eachother, overloads in one are
unrelated to the other in their entirety.
What I mean to say is that, given these classes
class A
virtual Method()
class B derives from A
class C derives from B
overrides Method
if a method defined in B calls base.Method() A::Method *will* be called
always. But if the method in B just calls Method(), the method will be
called virtually and the most derived method will be chosen. Which method is
called depends on the instance its called on.
If the instance is of class B then A::Method() will be called.
If the instance is of class C, then C::Method() will be called since in
class C Method is overridden.
ANother point I feel I should make is given
class A
virtual Method
class B derives from A
overrides Method
class C derives from A
class C will use A::Method() as class B has no relationship to C outside of
common ancestry. C is independent and overrides in B and in any classes
derived from B do no effect C in any way. To think of this in familial terms
you could say that it would be the same as your sister buying a boat: just
because you have the same parents doesn't mean the boat is yours as well.
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote: If so, here's another quote: "If necessary, methods or properties can make calls to overridden members in the base class by prefacing the method or property name with the keyword 'base.'"
In other words, if base class A contained public virtual int Method(), and B contained public override int Method(), then for C, if I want to make use of the method in class A, do I have to write it as: base.Method()? If so, why would I need to do that if C already inherits all public methods from A? Couldn't I just use Method() without the base prefix?
Yes, you could, in C, but only if you don't mind if further descendents override the method. Calling base.Method() is basically saying "Hey, I'm calling A::Method() precisely" whereas just calling Method() is saying "I'm calling the most recent override of the method Method()"
No - if you call base.Method() from C, you'll still get B's
implementation of Method, not A's.
base() doesn't go up one override, it goes up one class - in other
words, call whathever would be called if I just used Method() from the
immediate base class (C in this case).
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote: > If so, here's another quote: "If necessary, methods or properties can > make calls to overridden members in the base class by prefacing the > method or property name with the keyword 'base.'" > > In other words, if base class A contained public virtual int Method(), > and B contained public override int Method(), then for C, if I want to > make use of the method in class A, do I have to write it as: > base.Method()? If so, why would I need to do that if C already > inherits all public methods from A? Couldn't I just use Method() > without the base prefix? Yes, you could, in C, but only if you don't mind if further descendents override the method. Calling base.Method() is basically saying "Hey, I'm calling A::Method() precisely" whereas just calling Method() is saying "I'm calling the most recent override of the method Method()"
No - if you call base.Method() from C, you'll still get B's implementation of Method, not A's.
base() doesn't go up one override, it goes up one class - in other words, call whathever would be called if I just used Method() from the immediate base class (C in this case).
Hmm, my understanding of the class hiearchy was something like
B : A
C : A
Did I misread waht he said originally? -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Daniel O'Connell [C# MVP] wrote: class C will use A::Method() as class B has no relationship to C outside of common ancestry. C is independent and overrides in B and in any classes derived from B do no effect C in any way. To think of this in familial terms you could say that it would be the same as your sister buying a boat: just because you have the same parents doesn't mean the boat is yours as well.
Just to clarify, my original example was that both B and C are
subclasses of A.
Now, given what you said above, what's the difference between calling
Method() in C as opposed to base.Method()? Won't they both call the
Method method from A?
Jon Skeet [C# MVP] wrote: No - if you call base.Method() from C, you'll still get B's implementation of Method, not A's.
That confuses me. Why would C call a method from B? C is derived from A,
so I'm guessing it has no relation to B at all.
"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:eq********************@rcn.net... Daniel O'Connell [C# MVP] wrote:
class C will use A::Method() as class B has no relationship to C outside of common ancestry. C is independent and overrides in B and in any classes derived from B do no effect C in any way. To think of this in familial terms you could say that it would be the same as your sister buying a boat: just because you have the same parents doesn't mean the boat is yours as well.
Just to clarify, my original example was that both B and C are subclasses of A.
Now, given what you said above, what's the difference between calling Method() in C as opposed to base.Method()? Won't they both call the Method method from A?
The difference is that if you had class D that derives from C which
overrides Method, calling Method in C will call D::Method on instances of D
and A::Method on instances of C, while base.Method() will call A::Method()
every time.
you can run the following code for an example of this:
using System;
public class TestClass
{
public static void Main()
{
B b = new B();
C c = new C();
Console.WriteLine("calling on an instance of B");
b.TestMethod();
Console.WriteLine("calling on an instance of C");
c.TestMethod();
}
}
class A
{
public virtual void Method()
{
Console.WriteLine("A");
}
}
class B : A
{
public override void Method()
{
Console.WriteLine("B");
}
public void TestMethod()
{
Console.WriteLine("calling Method()");
Method();
Console.WriteLine("calling base.Method()");
base.Method();
}
}
class C : B
{
public override void Method()
{
Console.WriteLine("C");
}
}
Daniel O'Connell [C# MVP] wrote: The difference is that if you had class D that derives from C which overrides Method, calling Method in C will call D::Method on instances of D and A::Method on instances of C, while base.Method() will call A::Method() every time.
Ah, that makes some more sense now!
"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:G7********************@rcn.net... Daniel O'Connell [C# MVP] wrote:
The difference is that if you had class D that derives from C which overrides Method, calling Method in C will call D::Method on instances of D and A::Method on instances of C, while base.Method() will call A::Method() every time.
Ah, that makes some more sense now!
Glad I was able to help, ;)
Daniel O'Connell [C# MVP] wrote: "John Salerno" <jo******@NOSPAMgmail.com> wrote in message news:G7********************@rcn.net...
Daniel O'Connell [C# MVP] wrote:
The difference is that if you had class D that derives from C which overrides Method, calling Method in C will call D::Method on instances of D and A::Method on instances of C, while base.Method() will call A::Method() every time.
Ah, that makes some more sense now!
Glad I was able to help, ;)
Yeah, thanks. I just hate having to move on in a book when I don't fully
understand something I just read.
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote: base() doesn't go up one override, it goes up one class - in other words, call whathever would be called if I just used Method() from the immediate base class (C in this case).
Hmm, my understanding of the class hiearchy was something like
B : A C : A
Did I misread waht he said originally?
Nope, looks like I did.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
John Salerno <jo******@NOSPAMgmail.com> wrote: Jon Skeet [C# MVP] wrote:
No - if you call base.Method() from C, you'll still get B's implementation of Method, not A's.
That confuses me. Why would C call a method from B? C is derived from A, so I'm guessing it has no relation to B at all.
I misunderstood your post - I thought C was derived from B.
(This is why short but complete code helps - something concrete rather
than a description. Your description was entirely accurate and the
error was definitely mine, but it was easier to make than it would have
been with real code.)
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP] wrote: John Salerno <jo******@NOSPAMgmail.com> wrote:
Jon Skeet [C# MVP] wrote:
No - if you call base.Method() from C, you'll still get B's implementation of Method, not A's.
That confuses me. Why would C call a method from B? C is derived from A, so I'm guessing it has no relation to B at all.
I misunderstood your post - I thought C was derived from B.
(This is why short but complete code helps - something concrete rather than a description. Your description was entirely accurate and the error was definitely mine, but it was easier to make than it would have been with real code.)
Sorry, I didn't have any real code, except for the full program in the
book. But next time I'll post that along with the question anyway.
John Salerno <jo******@NOSPAMgmail.com> wrote: I misunderstood your post - I thought C was derived from B.
(This is why short but complete code helps - something concrete rather than a description. Your description was entirely accurate and the error was definitely mine, but it was easier to make than it would have been with real code.)
Sorry, I didn't have any real code, except for the full program in the book. But next time I'll post that along with the question anyway.
The trick is to be able to take a full program that does more than you
need to demonstrate and reduce it to *just* the bits that are
absolutely required to show what you mean. It's a very worthwhile
process, even if you end up throwing the code away afterwards (which
you usually will).
--
Jon Skeet - <sk***@pobox.com> 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: 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...
|
by: Martin Vorbrodt |
last post by:
Example code in one of my books intrigues me:
class B {
public:
B* Clone() const {
B* p = DoClone();
assert(typeid(*p) == typeid(*this));
return p;
}
|
by: Adrian Herscu |
last post by:
Hi all,
In which circumstances it is appropriate to declare methods as non-virtual?
Thanx,
Adrian.
|
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...
|
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...
|
by: Ken Brady |
last post by:
I'm on a team building some class libraries to be used by many other
projects.
Some members of our team insist that "All public methods should be virtual"
just in case "anything needs to be...
|
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...
|
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...
|
by: David Zha0 |
last post by:
Hi,
"when we call a virtual method, the runtime will check the instance who
called the method and then choose the suitable override method, this may
causes the performance drop down", is this...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
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)...
| |