F
Frank
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
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