Hi all,
Can somebody explain me what is the difference between overriding and shadowing with suitable examples.
Thanks
Difference between overriding and shadowing ?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.


Adavesh Managaon9 reputationPosted Jun 6, 2010, 7:38 AM EDT
class A
{
protected virtual void Foo()
{
MessageBox.Show("A.Foo");
}
}
class B : A
{
protected override void Foo()
{
base.Foo(); //Calls A's Foo method
MessageBox.Show("B");
}
}
Whereas in Shadowing, you define completely providing new definition for the parent class Method.
class A
{
public void Foo()
{
MessageBox.Show("A");
}
}
class B : A
{
public void Foo()
{
MessageBox.Show("B");
}
}
class X
{
new B().Foo();
}
When you compile the above code, the compiler will give a Warning message saying "B.Foo()' hides inherited member 'DbApp.A.Foo()'. Use the new keyword if hiding was intended."
If your warnings are not set to be treated as errors, then the above code will still override the base Foo method and shows "B".
I think you got the idea.
Satyapriya NayakRank #47 · 31.5K reputationPosted Jun 7, 2010, 4:10 AM EDT
Thanks for explaining.