Hi all,
Can somebody explain me what is the difference between overriding and shadowing with suitable examples.
Thanks
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Jun 7, 2010, 8:10 AM
Thanks for explaining.
Adavesh ManagaonPosted Jun 6, 2010, 11:38 AM
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.