c# - There is override and hidden method exist in derived class. Why? -
there code sample:
public class { public virtual void m() { console.writeline("a"); } } public class b : { public override void m() { console.writeline("b"); } } public class c : b { public new virtual void m() { console.writeline("c"); } } public class d : c { public override void m() { console.writeline("d"); } } internal class program { static void main() { c c = new c(); foreach (var method in typeof (c).getmethods().where(_ => _.name == "m")) { method.invoke(c, new object[0]); } c = new d(); foreach (var method in typeof(d).getmethods().where(_ => _.name == "m")) { method.invoke(c, new object[0]); } } }
i got overridden method exists in hierarchy in case hidden next ierarchy level class. can explain me why? or give me link can find out? thanks.
that's idea of hiding - still exists.
if c# wouldn't allow kind of hiding, they'd have disallow using method names if exist in base class.
the base class method still exist; otherwise base class no longer function properly. can't access directly class instance derives it:
c c = new c(); c.m(); // returns c ((b)c).m(); // returns b
Comments
Post a Comment