virtual and override methods

J

John Salerno

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!
 
D

Daniel O'Connell [C# MVP]

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.
 
J

John Salerno

Daniel said:
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?
 
D

Daniel O'Connell [C# MVP]

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.
 
J

Jon Skeet [C# MVP]

Daniel O'Connell said:
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).
 
D

Daniel O'Connell [C# MVP]

Jon Skeet said:
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?
 
J

John Salerno

Daniel said:
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?
 
J

John Salerno

Jon said:
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.
 
D

Daniel O'Connell [C# MVP]

John Salerno said:
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");
}
}
 
J

John Salerno

Daniel said:
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!
 
J

John Salerno

Daniel said:
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.
 
J

Jon Skeet [C# MVP]

Daniel O'Connell said:
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.
 
J

Jon Skeet [C# MVP]

John Salerno said:
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.)
 
J

John Salerno

Jon said:
John Salerno said:
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.
 
J

Jon Skeet [C# MVP]

John Salerno said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top