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).

see: differences between shadowing , overriding


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - How to Hide Date Menu from Datepicker in yii2 -