473,401 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Calling base for an inherited method?

I'm using inheritance more than I used to, and I find myself calling a lot
of base class methods. I generally call a base method from a dreived class
like this:

this.MyMethod();

I'm finding it somewhat confusing when I look at the code later, because I
expect to find a method called MyMethod() in the derived class.

I think C# would let me call a base class method from a derived class like
this:

base.MyMethod();

This would make it clear that the method being called is a base class
method. From the standpoint of clarity, I like it.

But, are there other reasons I couldn't call a base class method like that?
Reasons why I shouldn't? Thanks.

Dave Veeneman
Foresight systems
Nov 16 '05 #1
5 19997
Dave Veeneman <da****@nospam.com> wrote:
I'm using inheritance more than I used to, and I find myself calling a lot
of base class methods. I generally call a base method from a dreived class
like this:

this.MyMethod();

I'm finding it somewhat confusing when I look at the code later, because I
expect to find a method called MyMethod() in the derived class.

I think C# would let me call a base class method from a derived class like
this:

base.MyMethod();

This would make it clear that the method being called is a base class
method. From the standpoint of clarity, I like it.

But, are there other reasons I couldn't call a base class method like that?
Reasons why I shouldn't? Thanks.


Well, it depends whether you want to call the overridden version if
there is one. If you absolutely know when you write the code that you
don't want to call an overridden version, call base.MyMethod(). If you
want to use whatever version has been provided by the class, call
this.MyMethod() or just MyMethod(). I personally rarely use
base.MyMethod() outside code which is itself overriding MyMethod.

--
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
I am assuming MyMethod is something defined in the base class but *not*
overridden in the inheriting class? In that case, these are the same.

However, if MyMethod is overridden in the inheriting class, then these 2
things are completely different. The first one calls the overridden version,
and the second one calls the base version.

So you want to be careful in how you typically call these methods, to make
sure you are calling the one you really want.

In the case where MyMethod may or may not be overridden, it seems that
always calling this.MyMethod is safest. If there is a version in the
inheriting clas - that will get called. If not, the base version will get
called. So I would only use 'base' to prefix things where you explicitly
want to call the base class's version for whatever reason, regardless of
whether or not there is a version in the inheriting class.

"Dave Veeneman" <da****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I'm using inheritance more than I used to, and I find myself calling a lot
of base class methods. I generally call a base method from a dreived class
like this:

this.MyMethod();

I'm finding it somewhat confusing when I look at the code later, because I
expect to find a method called MyMethod() in the derived class.

I think C# would let me call a base class method from a derived class like
this:

base.MyMethod();

This would make it clear that the method being called is a base class
method. From the standpoint of clarity, I like it.

But, are there other reasons I couldn't call a base class method like that? Reasons why I shouldn't? Thanks.

Dave Veeneman
Foresight systems

Nov 16 '05 #3
The one place you may get into problems will be with future development.
Assume that you have class A and B where B is a child class of A. In B, you
have several calls to base.MyMethod(), which is defined in A. Later on, you
come back to your code and add an override of MyMethod() in B. If you
forget to change your code, all of the rest of B will be calling the A
version of MyMethod() instead of the one you defined.

Like wise, if someone comes along and declares a class C that is a child of
B you will get some unexpected behavior. If they wish to perform some
action every time that MyMethod() is called, they would expect to be able to
override MyMethod() in their class. However your code is calling
base.MyMethod() so C will not be able to intercept any of hte calls that B
makes.

--
Jared Parson [MSFT]
ja******@online.microsoft.com

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Dave Veeneman" <da****@nospam.com> wrote in message
news:#8**************@TK2MSFTNGP09.phx.gbl...
I'm using inheritance more than I used to, and I find myself calling a lot
of base class methods. I generally call a base method from a dreived class
like this:

this.MyMethod();

I'm finding it somewhat confusing when I look at the code later, because I
expect to find a method called MyMethod() in the derived class.

I think C# would let me call a base class method from a derived class like
this:

base.MyMethod();

This would make it clear that the method being called is a base class
method. From the standpoint of clarity, I like it.

But, are there other reasons I couldn't call a base class method like that? Reasons why I shouldn't? Thanks.

Dave Veeneman
Foresight systems

Nov 16 '05 #4


"Dave Veeneman" wrote:
I'm using inheritance more than I used to, and I find myself calling a lot
of base class methods. I generally call a base method from a dreived class
like this:

this.MyMethod();
that is not how you call a base class method. it won't work if you override or hide the base method with a new definition in your derived class.

I'm finding it somewhat confusing when I look at the code later, because I
expect to find a method called MyMethod() in the derived class.

I think C# would let me call a base class method from a derived class like
this:

base.MyMethod();

This would make it clear that the method being called is a base class
method. From the standpoint of clarity, I like it.

But, are there other reasons I couldn't call a base class method like that?
no. it's perfectly valid syntax.
Reasons why I shouldn't? Thanks.
nope.

Dave Veeneman
Foresight systems

Nov 16 '05 #5
Marina <so*****@nospam.com> wrote:
I am assuming MyMethod is something defined in the base class but *not*
overridden in the inheriting class? In that case, these are the same.
I'm not disagreeing with you, but it's worth just pointing out that it
could also be overridden in a class which derives from the one in which
the call is made, in which case it still makes a difference. You need
to know in advance which version you want to call if and when the
method is overridden in either a derived class or at a later date in
this class.
So you want to be careful in how you typically call these methods, to make
sure you are calling the one you really want.
Agreed.
In the case where MyMethod may or may not be overridden, it seems that
always calling this.MyMethod is safest. If there is a version in the
inheriting clas - that will get called. If not, the base version will get
called. So I would only use 'base' to prefix things where you explicitly
want to call the base class's version for whatever reason, regardless of
whether or not there is a version in the inheriting class.


Absolutely.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

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

Similar topics

4
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final...
4
by: Claire | last post by:
I'm having real brain failure today. I've done this lots of times with constructors but not with virtual methods and the compiler complains because Ive put the :base(foo) after the function...
7
by: Santi | last post by:
I have two classes: Product and Fruit which inherits from Product. If I try to narrow the reference to the base type by a cast, I always get a reference to the inherited type. For example: ...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
3
by: Matt F. | last post by:
I have an abstract class that about a dozen sub-classes inherit from. I want to enforce that each sub-class shadows an event in the abstract class, but can't quite figure out how to do this. ...
8
by: Mike C# | last post by:
Suppose I have a base class "foo". Another class, "bar" derives from it. Base class "foo" has a method called "rob_the_liquor_store()", and the inherited class "bar" overrides this method with one...
5
by: Eliseu Rodrigues | last post by:
Hi I would like to have a static method on a base class that executes some action (for example retrieves the row count) on a table whose name is the same of the inherited class name. For...
10
by: Finger.Octopus | last post by:
Hello, I have been trying to call the super constructor from my derived class but its not working as expected. See the code: class HTMLMain: def __init__(self): self.text = "<HTML><BODY>";...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
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,...

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.