c# - base.Method() with multiple levels of inheritance not being called? -


i've searched , not been able find solution problem. scenario simple:

public class {     public virtual void methodone()     {        console.log( "a" );      } }  public class b : {     public override void methodone()     {         base.methodone();         console.log( "b" );     } }  public class c : b {     public override void methodone()     {         base.methodone();         console.log( "c" );     } } 

what trying have instance of class c (we'll name 'instancec') call both overridden method of parent, , grandparent. i'd expect this:

instancec.methodone(); // output: // "a" // "b" // "c" 

but instead getting this:

instancec.methodone(); // output // "a" // "c" 

with class b's method being skipped over. not possible? thought whole point of inheritance/polymorphism. in advance!

your example works expected me. see b c. think issue c doesn't extend b. however, let me suggest arguably safer pattern while we're on subject. seem want overrides of methodone execute code base classes. great, inheritance pattern this. however, pattern cannot force inheritors execute base logic because cannot force them call base.methodone(). if callbase.methodone(), cannot ensure order of logic. call base.methodone() @ beginning of method, middle of method, or end of method? often, in these types of patterns want sub classes execute base logic @ beginning of function. following pattern forces inheritors execute base logic in order base classes expect. it's technically less flexible safer because inheritors must extend base classes in way base classes specify.

public class {     //don't make method virtual because don't want inheritors      //to able override functionality.  instead, want inheritors     //to able append functionality.     public void methodone()     {         console.writeline( "a" );          methodtobeoverriddenone();     }     //expose place inheritors can add functionality     protected virtual void methodtobeoverriddenone() { }       }  public class b : {     //seal method because don't want inheritors      //to able override functionality.  instead, want inheritors     //to able append functionality.     protected sealed override void methodtobeoverriddenone()     {         console.writeline("b");         methodtobeoverriddentwo();     }     //expose place inheritors can add functionality     protected virtual void methodtobeoverriddentwo() { }   }  public class c : b {     protected sealed override void methodtobeoverriddentwo()     {         console.writeline("c");     } } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -