Overriding in C#, making two subsequent method virtual -
it's question of overriding in c#.
when use following code:
class program { class { public virtual void callme() { console.writeline("this a"); } } class b : { public new virtual void callme() { console.writeline("this b"); } } class c : b { public override void callme() { console.writeline("this c"); } } static void main(string[] args) { obj = new c(); obj.callme(); console.readkey(); } }
output: this a
and when use:
class program { class { public virtual void callme() { console.writeline("this a."); } } class b : { public override void callme() { console.writeline("this b."); } } class c : b { public override void callme() { console.writeline("this c."); } } static void main(string[] args) { obj = new c(); obj.callme(); console.readkey(); } }
output: this c.
so if make method virtual in subsequent classes (a
b
), why calls last method, , if b
class overriding a
, c
overriding b
it's calling c
's method.
please explain.
in 1st case, b shadowing , c overriding shadow in b. long reference type a, call method.
in 2nd case b overriding , c overriding b (so it's overriding a).
Comments
Post a Comment