473,597 Members | 2,301 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance woes

This is rediculous. It is said when you override a method in a derived
class that's implemented in the base class, you "override the base
class methods" implementation, correct? but in my test,
base.SomeMethod () still prints out it's *own* implimentation - that's
tottally different from the deriveds overrided method. I don't get it.
Really, What's the difference betwen this.SomeMethod () and
base.SomeMethod ()? more precisely, is *this* and *base* two different
things, meaning they both reside at a different memory address and each
have their *own* seperate members, again meaning; different address
space? To me it looks like a derived class has two seperate members for
each inherited member in the basde class, one in the derived class
which can be accessed with *this* and the other completely unrelated
member, which can be accessed with *base*. I hope someone understands
my frustration and helps me untangle this confusion.

Thanks to all who reply.

Jan 20 '06 #1
5 1313
"this" is the current implementation (in your case, the derived class).
"base" is the base class. The difference is that when you call
base.SomeMethod () you're calling the base class's implementation of the
method, rather than the override.

When you inherit a class, you inherit all of it. Overriding a method doesn't
overWRITE the method. It's still there. And believe it or not, there are
plenty of cases in which you will *need* it to be there. However, be aware
that only the derived class instance itself will be able to call the Base
class method. It still exposes only one (the overridden) implementation.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"relient" <xl************ ***@gmail.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
This is rediculous. It is said when you override a method in a derived
class that's implemented in the base class, you "override the base
class methods" implementation, correct? but in my test,
base.SomeMethod () still prints out it's *own* implimentation - that's
tottally different from the deriveds overrided method. I don't get it.
Really, What's the difference betwen this.SomeMethod () and
base.SomeMethod ()? more precisely, is *this* and *base* two different
things, meaning they both reside at a different memory address and each
have their *own* seperate members, again meaning; different address
space? To me it looks like a derived class has two seperate members for
each inherited member in the basde class, one in the derived class
which can be accessed with *this* and the other completely unrelated
member, which can be accessed with *base*. I hope someone understands
my frustration and helps me untangle this confusion.

Thanks to all who reply.

Jan 20 '06 #2
One way to look at it is that when you override a method you are actually
"adding" to the code that "can" be executed by that method.

Here is an example (kind of lame one and may not even even compile) that may
help you see why it is usefull.

class FourSidedBox
{
public int width;
public int height;

public void Draw()
{
DrawRectangle(w idth,height);
}
}

class Square: FourSidedBox
{
public override void Draw()
{
width = 10;
height = 10;
base.Draw();
}
}

class Rectangle: FourSidedBox
{
public override void Draw()
{
width = 20;
height = 10;
base.Draw();
}
}

Both of the derived classes (Square and Rectangle) both "add" new
functionallity to the Draw() method - they set different width and height
values. They then call the base.Draw() method to actually perform the
original code as well.

This also helps you reuse the code in the original Draw() method so you do
not have to include it in every derived class.

Lastly.. what if you wanted to change from DrawRectangle() to
DrawFilledRecta ngle().. You would only have to change it in one place!

Hope this helps shed some light on how and why it works the way it does..

And as Martha says... "It's a good thing..."

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:u8******** ******@TK2MSFTN GP15.phx.gbl...
"this" is the current implementation (in your case, the derived class).
"base" is the base class. The difference is that when you call
base.SomeMethod () you're calling the base class's implementation of the
method, rather than the override.

When you inherit a class, you inherit all of it. Overriding a method
doesn't overWRITE the method. It's still there. And believe it or not,
there are plenty of cases in which you will *need* it to be there.
However, be aware that only the derived class instance itself will be able
to call the Base class method. It still exposes only one (the overridden)
implementation.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"relient" <xl************ ***@gmail.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
This is rediculous. It is said when you override a method in a derived
class that's implemented in the base class, you "override the base
class methods" implementation, correct? but in my test,
base.SomeMethod () still prints out it's *own* implimentation - that's
tottally different from the deriveds overrided method. I don't get it.
Really, What's the difference betwen this.SomeMethod () and
base.SomeMethod ()? more precisely, is *this* and *base* two different
things, meaning they both reside at a different memory address and each
have their *own* seperate members, again meaning; different address
space? To me it looks like a derived class has two seperate members for
each inherited member in the basde class, one in the derived class
which can be accessed with *this* and the other completely unrelated
member, which can be accessed with *base*. I hope someone understands
my frustration and helps me untangle this confusion.

Thanks to all who reply.


Jan 20 '06 #3
The first thing you have to distinguish is the difference between
instance data and code. You get a new copy of instance data every time
you construct a new instance of your class (via new), but there is only
one copy of a class's code, which resides in a different place from the
instance data. So, a running program will have many instances of the
data members (aka state aka fields) associated with a class stored on
the heap, but only one, unchanging copy of the code. I know that seems
irrelevant to your question, but bear with me.

"this" and "base" can be used to refer to two different things:
instance data (i.e. fields), and code (e.g. properties, methods). What
goes on under the covers is different in the two cases.

Take a look at this example:

public class A
{
protected int _aField;
}

public class B : A
{
private int _bField;
}

The one you didn't ask about was data. Remember that when you
instantiate a new instance of a derived class (B), the object created
on the heap will have _two_ fields in it: _aField and _bField. When you
say "this._bFie ld" in the code, you're referring to the field declared
in class B ("this"). When you say "this._aFie ld" (or "base._aFie ld")
then you're referring to the field declared in class A, the base class
("base"). Since the fields have different names, you can use "this" or
"base" to refer to _aField and both will work, because the base class's
protected and public fields "show through" as though they were declared
in the derived class.

Conceptually, using the "base." prefix with data fields just means that
you're restricting the fields you want to work with (the names that
follow "base.") to those declared in the base class or below.

Code works a little differently. Remember that there is only one copy
of code, regardless of how many objects you instantiate. Expand the
example above to include a method:

public class A
{
protected int _aField;

public virtual void AMethod() { }

public virtual void AMethod2() { }
}

public class B : A
{
private int _bField;

public override void AMethod() { }
}

Now, there is separate _code_ for each of these classes. There is
compiled code for class A, containing a method called AMethod(). There
is also separate compiled code for class B, also containing a method
called AMethod(). Whether these two compiled classes exist in the same
assembly or different assemblies, the important point is that there two
separate compiled bits of code, one for each class.

The compiled code for class B refers to class A, though, because class
B knows it's derived from class A, and may need to execute code from
there from time to time.

Within class B, the derived class, whenever you say "this." followed by
a reference to some code, you may be referencing a method in class B
(if there's one by that name defined there), or you may be referencing
something defined in class A (if there's something in class A by that
name and it hasn't been overridden in the derived class, B). So, in the
example above, if you say "this.AMethod() " then you're talking about
B's AMethod, because it defines something by that name. If you say
"this.AMethod2( )" then you're talking about A's AMethod2, because the
derived class, B, doesn't define anything called AMethod2, so the
compiler goes up the class hierarchy looking to resolve the name. In
effect, "this." could result in a call to code in the compiled class B
_or_ the compiled class A, depending upon how the compiler resolves the
name.

So, finally, what's "base" all about? "base" tells the compiler that
you want to _ignore_ what's defined in the derived class (B) and _only_
look up the class hierarchy. So, if you say "base.AMeth od", then the
search to resolve the name starts in class A (the base class of B).

The language has to provide this feature: what if you want to override
AMethod in class B, but you want to do a little bit of work _in
addition to_ what class A's AMethod does? The only way to do this is to
say:

public class B : A
{
public override void AMethod()
{
... do some stuff ...
base.AMethod(); // Call my base class's (A's) "AMethod"
... do some other stuff ...
}
}

The important point is that this works _only_ from _within_ a class's
code. Classes outside B can't do this:

B b = new B();
b.base.AMethod( );

or anything like that. From _outside_ the class B, the only method
called AMethod that the "public" can see is the one defined in B. B,
however, has access to its parent class's AMethod(), in order to do
what you saw up above.

And no, the capability doesn't extend up the hierarchy. There's no such
thing as "base.base" . So, a class C that looked like this:

public class C : B
{
public override void AMethod()
{ }
}

could not call A's AMethod directly, only B's (by saying
base.AMethod()) .

Jan 21 '06 #4
Bruce Wood wrote:
<quote>helpfu l stuff</quote>

OK, first let me say "Thanks a lot" to, Bruce Wood. Impressive,
detailed - precisely what I needed to know.

Let me just do an overview of this to brush off any misconceptions and
or misunderstandin gs I, may still have:

An overview:
I know that when you invoke SomeMethod from an instantiated object, the
compiler secretly passes the object invoking the method as the last
argument besides the ones already defined (if any), and, this object is
referenced in the method via the "this" keyword. I also know that When
you create many instances of an object, you create seperate data
(related only to it's instantiated object), and not the many others
created. But, "code", which is methods, properties, indexers etc.. are
only created once (perhaps during the instantiation of the first
object?). But the trick to use different data on the same "code" (which
gives the impression that *n* methods are created for every *n* objects
you create/instantiate) is by passing the object which is invoking the
"code" and that's how all instances work and relate to the same piece
of "code" - I like that.

Now that, that was addressed, please let me know if I got any of it
wrong.
Now to the heart of the question which is in your second example:

/* But before I begin: just to be on the safe understanding side of
things; when you say *separate compiled bits of code*, I interperate
that as *different memory(s) location* - I come from a long C
background and, distinguishing code, data etc,. as "seperate memory" in
programming, I have found, helps me a lot in understanding things a
whole lot better :) */
public class A
{
protected int _aField;
public virtual void AMethod() { }
public virtual void AMethod2() { }
}
public class B : A
{
private int _bField;
public override void AMethod() { }
}

The "Code":
In the current state of the above two classes, I'm only going to refer
to the "code" (methods etc,.. not data; fields) for the moment. There
is three "seperate compiled bits code": There's two that are: AMethod()
and AMethod2(), which reside in the base class, and then there's the
overrided AMethod() which resides in the derived classs. These three
sit on seperate memory, so, base.AMethod() refers to the method defined
in the base class and this.AMethod() refers to the method defined in
the derived class (both sit on seperate memory but belong to the same
object), correct? *But*, if you change the current state of class B,
the derived class, by removing AMethod(), then there is *only* two
seperate compiled bits of code, and base.Method() and this.Method()
refer to, and only, the base Method() - both are the same piece of
"code" which sit on the same memory. Right?

The "Data":
The fields, _aField and _bField, are data. Since data, unlike code, is
created seperate for each object created, doing this._aField and
base._aField, refer to *the same memory*. Am I right, again? :D

Finally, one last question; quite different but still pertaining to the
topic. override vs new. Is the difference between the two really just
to serve as "self code documentation" (which gives other potential
programmers reading your code your true intention)?

That is all and I'd like to see a response from, Bruce if he gets the
chance to read my thread again, or anyone. And thanks again, Bruce, I
think you did a fantastic job in helping me understand how inheritance
really works.

Jan 22 '06 #5
relient <xl************ ***@gmail.com> wrote:

<snip>
Finally, one last question; quite different but still pertaining to the
topic. override vs new. Is the difference between the two really just
to serve as "self code documentation" (which gives other potential
programmers reading your code your true intention)?


No - there's a big, big difference between override and new. See
http://www.pobox.com/~skeet/csharp/faq/#override.new
for a brief description and example. Let me know if that isn't enough.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 22 '06 #6

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

Similar topics

7
1710
by: Mark | last post by:
O, woe is me, to have seen what I have seen, see what I see! (That's Shakespeare for those who were wondering what I'm on about) I am "having fun" with cookies. And I wonder if I have missed something obvious.
0
1509
by: Cedric | last post by:
This is a 3 weeks old problem, but having found a solution (and having looked for one here, finding only this message), I'm replying now. From: Jive (someone@microsoft.com) Subject: Upgrade woes: Numeric, gnuplot, and Python 2.4 Date: 2004-12-11 18:45:10 PST > Here's my sitch: > > I use gnuplot.py at work, platform Win32. > I want to upgrade to Python 2.4.
3
3446
by: Angel Cat | last post by:
Trying to get my jobs to send mail when job fails. Should be easy but it's giving me headache Had a whole slew of issues. Outlook is installed with a n outlook mail profile set up that can send mail in outlook. I can create a SendMail DTS and execute it to send mail Email works in these scenarios 1. I create a DTS package in SQL Server with just SendMail with the same Profile "ABC" and click Execute and it sends
2
1824
by: Andrew Thompson | last post by:
- NN 4.78 rendering woes, links at far left - I am trying to rework an old site, make it valid html and css, improving the x-browser and 'older browser' compatibility. My efforts so far, have been abysmal. See, for example, this screen shot of the very simple home page, it is not even 10% finished, but I have already run into problems. http://www.physci.org/test/xbrowser/nn478home01.png
0
2088
by: Arun Bhalla | last post by:
I'm having some inconsistency problems with my deployment project ("Setup") and its custom actions ("Installer"). I'm using Visual Studio .NET 2003 (.NET 1.1, no service pack) on Windows XPSP1. My main project is a band object (Explorer Bar) in an assembly (.DLL) that needs to be registered on installation and unregisted on uninstallation. In addition, during installation, Setup creates registry subkeys, sets registry values, installs...
4
2889
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
22
23337
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
5
1434
by: rkozlin | last post by:
Running into an issue where the compiler will throw an error... "The type '<BaseClass>' is defined in an assembly that is not referenced. You must add a reference to assembly '<BaseClass>'." .... when using a derived class where the base class and the derived class are compiled into separate assemblies. In this case we are attempting to reference the derived class in a project and had assumed that the base class assembly would...
9
1542
by: Mark Rae | last post by:
Hi, This time, I'm looking for a regular expression which says "the string must contain exactly seven or exactly eight digits" e.g. 123456 fails 1234567 passes 12345678 passes 123456789 fails
0
8276
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...
1
8040
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6698
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
5847
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
5436
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3889
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
3932
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1243
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.