<ti************ ****@yahoo.com> wrote...
I'm curious as to the need for the new keyword in
an inherited method redefinition,
Well, it's actually not *needed*, but gets rid of the compiler warnings...
;-)
When you write the "new" keyword, you have told the compiler that you're
*conciously* hiding the inherited method.
So, it won't make any difference when running the application whether you
have used "new" or not.
What really differs, is whether you're using "override" or not!
so -
Suppose I have a class hierachy that looks like
public class Base
{
public virtual void myMethod()
{...
}
}
public class Derived : Base
{
public void myMethod()
{...
}
}
From what I understand, without the override keyword the
compiler produces a warning and the Base class version is
hidden, meaning that if I tried
Base myObject = new Derived;
myObject.myMeth od();
Would this produce a compile time (or runtime) error?
Neither.
That will use the method defined in Base.
But..
Derived myObject = new Derived();
myObject.myMeth od();
....will use the method defined in Derived.
Or maybe this just means that myObject cannot be used
polymorphically as in -
Base myObject1 = new Base;
Base myObject2 = new Derived;
myObject1 = myObject2;
Correct. Non-overridden methods will use the implementation based on the
reference type, in this case the type of the variable 'myObject2': Base.
Now if instead of override I use new, all this apparently
does is get rid of the compiler warning. I'm confused.
There's a big difference between overriding and hiding.
To make it simple we could say that when you override a method, the method
gets connected to the type of the instance, while when you're "hiding" a
method, the method is connected to the type of the *reference* to the
instance.
public class Base
{
public virtual void myMethod() {}
}
public class Hider : Base
{
// "new" keyword eliminates compiler warnings
// If you can live with them, "new" isn't necessary...
public new void myMethod() { }
}
public class Overrider : Base
{
public override void myMethod() { }
}
------------------------------
Here the reference type matters:
Base bh = new Hider();
Hider hh = new Hider();
bh.myMethod(); // Runs myMethod from Base
hh.myMethod(); // Runs myMethod from Hider
------------------------------
Here the type of the instance matters.
Base bo = new Overrider();
Overrider oo = new Overrider();
bo.myMethod(); // Runs myMethod from Overrider
oo.myMethod(); // Runs myMethod from Overrider
------------------------------
/// Bjorn A