class parent
{
public parent()
{
}
public string Show()
{
return "I am parent class";
}
}
class child : parent
{
public new string Show()
{
return "I am Child class";
}
}
protected void Page_Load(object sender, EventArgs e)
{
parent p=new parent();
Response.Write(p.Show()); --parent is called-- this is fine.
child c = new child();
Response.Write(c.Show());-- child is called inspite of child having new keyword.(here when the child is hidden it should call the parent method something like child class passing values
to parent using base keyword.)
parent pp = new child();
Response.Write(pp.Show());--parent is called.-- this is fine as we intentionally did hide the child method with new so parent is called
//child cc = new parent();
//Response.Write(cc.Show());-- casting problem child class ref variable cannot refer to an object(instance) of parent class.
}
using New (Question1): Even if i use the New keyword in child class for show method, parent method is called and even if i dont use the new keyword then also parent method is called.in that case
why should i use new keyword for the child class? this is called method hiding in c# and shadowing in vb.net.
using New (question 2): When i have new for child method and the child has inherited the parent class method then instead of calling the parent method i get the child method
child c = new child();
Response.Write(c.Show());-- child is called inspite of child having new keyword.
Using Virtual :where as if i over ride the same child class method subject to the condition parent method is virtual then child is called and if i dont over ride the child inspite of parent being
virutal parent is called this is method over riding.
what is the real use of new ?