473,836 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

virtual and override methods

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!
Nov 17 '05 #1
15 2556
> 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.
Nov 17 '05 #2
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?
Nov 17 '05 #3
>>

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.
Nov 17 '05 #4
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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #6
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?
Nov 17 '05 #7
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.
Nov 17 '05 #8

"John Salerno" <jo******@NOSPA Mgmail.com> wrote in message
news:eq******** ************@rc n.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.WriteLi ne("calling on an instance of B");
b.TestMethod();
Console.WriteLi ne("calling on an instance of C");
c.TestMethod();
}
}
class A
{
public virtual void Method()
{
Console.WriteLi ne("A");
}
}

class B : A
{
public override void Method()
{
Console.WriteLi ne("B");
}

public void TestMethod()
{
Console.WriteLi ne("calling Method()");
Method();
Console.WriteLi ne("calling base.Method()") ;
base.Method();
}
}

class C : B
{
public override void Method()
{
Console.WriteLi ne("C");
}
}
Nov 17 '05 #9
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!
Nov 17 '05 #10

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

Similar topics

20
1789
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.
10
7325
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; }
32
4535
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
4
2228
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 bool Equals(object obj) {} Another is using the override keyword, like: public override bool Equals(object obj) {}
14
12165
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 events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years namespace MyNamespac public delegate void MyDel() public class MyBase public virtual...
175
8925
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 changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
5
2616
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 classes yClass which inherits xClass. xClass has a virtual method which simply writes a line of text stating its origin, yClass implements the same method which writes a line of text stating its origin also (i.e. "From yClass"). I ran the...
5
4537
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 I've to do by using override. It's also written, that's possible to "hide" the base class method by using the new key word. Because I've already written some C# code and I didn't know anything
4
6571
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 right? And, why not use "new" instead of using "virtual"? And the last question, what is the differences between a abstract method and a interface?
0
9825
marktang
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...
0
10852
Oralloy
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...
0
10553
jinu1996
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...
0
9382
agi2029
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...
1
7793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5651
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...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4459
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
3
3116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.