In shadowing concept the child class will recreate a method with the same name as the override enabled method in parent class, and this new method is the available method for next level child classes instead of parent class's method.
private void button_Click(object sender, EventArgs e)
{
A a = new B();
a.Hello();
a.Mine();
}
class A
{
public void Hello()
{
MessageBox.Show("In A ");
}
public virtual void Mine()
{
MessageBox.Show("In A Mine");
}
}
class B : A
{
public new void Hello()
{
MessageBox.Show("In B ");
}
public override void Mine()
{
MessageBox.Show("In B Mine");
}
}
The parent class cannot see the newly created method in child class and because of the same name the class will call the method in the parent class.
This will surely create ambiguity in your mind about Overriding vs Shadowing for further details : Shadowing vs Overriding
private void button_Click(object sender, EventArgs e)
{
A a = new B();
a.Hello();
a.Mine();
}
class A
{
public void Hello()
{
MessageBox.Show("In A ");
}
public virtual void Mine()
{
MessageBox.Show("In A Mine");
}
}
class B : A
{
public new void Hello()
{
MessageBox.Show("In B ");
}
public override void Mine()
{
MessageBox.Show("In B Mine");
}
}
The parent class cannot see the newly created method in child class and because of the same name the class will call the method in the parent class.
This will surely create ambiguity in your mind about Overriding vs Shadowing for further details : Shadowing vs Overriding
No comments:
Post a Comment