473,396 Members | 1,678 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,396 software developers and data experts.

Fundamental OO Question

I'm relatively new to the OO game and for the most part feel I
understand it (at least at some level), however, something has me all
messed up.

Please consider the following:

public class Base
{
protected int _variable;

public Base(){}

public int SetVariable
{
set {_variable = value;}
}
}

// does nothing (in this example)
public class Derived : Base
{
public Derived() : base() {}
}

public class FurtherDerived : Derived
{
public FurtherDerived() : base() {}

public new int SetVariable
{
set {_variable = value * 2;}
}
}

public class Test
{
public Test()
{
bool someCondition = true;

Derived obj;

// Just to show why I may be doing this
if(someCondition)
obj = new Derived();
else
obj = new FurtherDerived();

// I want this to call the property accessor in FurtherDerived
// but instead it calls the accessor from Base.
obj.SetVariable = 5;
}
}

So, basically when I call the method in 'FurtherDerived' that
explicitly hides the base class' implementation I find that the base
class' method is still getting called.

I think I partially understand the mechanism by which this is happening
but I don't understand why its happening. Because I've created 'obj'
as a 'Derived' type, when the method call is made it goes to the
Derived class sees that there is no implementation for my method and
goes to the base class. However the part I don't understand is that
because I have defined my obj as 'FurtherDerived', when I instantiate
it the constuctor of 'FurtherDerived' gets called. If it knows to call
the constructor of FurtherDerived then why doesn't it go to the method
in FurtherDerived? Also, when I check the type of obj just before the
method call I can see that it is a FurtherDerived type.

So I'm not sure why the SetVariable method of the FurtherDerived class
doesn't get called.

Do I have to rewrite the above to look like this. And if so, why?:

public Test()
{
bool someCondition = true;

// Just to show why I may be doing this
if(someCondition)
Derived obj = new Derived();
else
FurtherDerived obj = new FurtherDerived();

// I want this to call the property accessor in FurtherDerived
// but instead it calls the accessor from Base.
obj.SetVariable = 5;
}
Thanks
Frank

Jan 5 '06 #1
3 953
>So, basically when I call the method in 'FurtherDerived' that
explicitly hides the base class' implementation I find that the base
class' method is still getting called.
Right, that's the behavior you get when you use the "new" modifier to
hide a base class member. It breaks the polymorphism so you can't call
the new member through a reference to a base class.

If it knows to call
the constructor of FurtherDerived then why doesn't it go to the method
in FurtherDerived?
You will always call the constructor of the exact type you're
creating. Constructors aren't inherited and you specify the type to
create so that's no problem.

It doesn't call FurtherDerived.SetVariable because you're calling
through a Derived reference and because you used the "new" modifier
FurtherDerived.SetVariable is a different member than
Derived.SetVariable (or actually Base.SetVariable).

Do I have to rewrite the above to look like this. And if so, why?:


No, that wouldn't even compile. What you probably want to do is to use
virtual/override instead of "new".

public class Base
{
public virtual int SetVariable
{
set {_variable = value;}
}
}

public class FurtherDerived : Derived
{
public override int SetVariable
{
set {_variable = value * 2;}
}
}
I should also mention that according to the .NET naming guidelines
your property name shouldn't start with "Set", it should just be
"Variable".
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 5 '06 #2
Mattias

Thanks for the quick response. As I was writing the message the
reasoning behind it became a little more clear but I just wanted to
confirm.

How does one decide when to use virtual/override or new? I mean in
this example its quite clear as there latter doesn't work but say I had
the SetVariable method in the Derived class - would I still use
virtual/override, or would I use new? In this case both would work
correct?

Thanks,
Frank

PS The SetVariable naming was simply for this example. Thanks for the
heads-up though.

Jan 5 '06 #3
>>Do I have to rewrite the above to look like this. And if so, why?:
No, that wouldn't even compile. What you probably want to do is to use
virtual/override instead of "new".

Just for more information, the OP can also cast:
if (someCondition)
obj.SetVariable = 5;
else
((FurtherDerived) obj).SetVariable = 5;

Jan 5 '06 #4

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

Similar topics

51
by: Casper Bang | last post by:
My question is fundamental I beleive but it has been teasing me for a while: I have two classes in my app. The first class is instantiated as a member of my second class. Within this first class,...
3
by: Kyle Kolander | last post by:
I recently looked over the faq item relating to fundamental type sizes: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.5 and was a bit surprised, as I had been taught more-or-less the...
8
by: Kyle Kolander | last post by:
Sorry, I sent this to comp.std.c++ and meant to send it here as well... Why are the minimum size guarantees for fundamental types intentionally omitted from section 3.9.1 Fundamental types of...
26
by: myName | last post by:
a && b || c is evaluated as (a && b) || c or it is evaluated as a && (b || c)
4
by: kaborka | last post by:
I have a WinForm with controls bound to a typed recordset that was generated by the Dataset Designer. There are several ComboBox controls with their DataSource bound to different lookup tables. ...
0
by: Gene Hubert | last post by:
Well, it seems fundamental to me anyway. Hopefully it is simple enough. The question is for when the source for the dragdrop is a different application that the target for the dragdrop. How...
8
by: maneeshkhare | last post by:
I have a doubt regarding the architecture, and working of the ASP.NET framework. I haven't been able to satisfy myself with any answer. I do understand that for each request for a resource...
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
1
by: bharathreddy | last post by:
This Article gives an introduction to VSTS Team Foundation & fundamental difference between Visual Source Safe (VSS) and VSTS Team Foundation. Team Foundation is a set of tools and technologies...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
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.