my question is quite stupid , but plz answer it.
when we inherit a base class then auto matically we can override all its function . all functions in the derived class , having same name as in base r taken as with keyword new
so i m basically confused then what is the differnce between new n override n y we need override at all bcoz we can override all base class function without keyword override
plz if u know some pdf r doc r link abt this plz provide that
i m jst a beginner
Loading
Jude LandryPosted May 1, 2006, 7:47 PM
Stan GershengorenPosted Apr 24, 2006, 7:34 PM
class base
{
public void f()
{
Console.WriteLine ("base");
}
}
class derived : base
{
public new void f()
{
Console.WriteLine ("derived");
}
}
now execute this code somewhere in the Main method
base d = new derived();
d.f();
in case of new "base" will be printed because the compiler will go by reference and not by instance
if you execute the same code but override the function instead you will see "derived" printed out, because base method no longer exists.
Hope that helps.