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

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -