c# - Why does calling a method in my derived class call the base class method? -


consider code:

class program {     static void main(string[] args)     {         person person = new teacher();         person.showinfo();         console.readline();     } }  public class person {     public void showinfo()     {         console.writeline("i person");     } } public class teacher : person {     public new void showinfo()     {         console.writeline("i teacher");     } } 

when run code, following outputted:

i person

however, can see instance of teacher, not of person. why code that?

there's difference between new , virtual/override.

you can imagine, class, when instantiated, nothing more table of pointers, pointing actual implementation of methods. following image should visualize pretty well:

illustration of method implementations

now there different ways, method can defined. each behaves different when used inheritance. standard way works image above illustrates. if want change behavior, can attach different keywords method.

1. abstract classes

the first 1 abstract. abstract methods point nowhere:

illustration of abstract classes

if class contains abstract members, needs marked abstract, otherwise compiler not compile application. cannot create instances of abstract classes, can inherit them , create instances of inherited classes , access them using base class definition. in example like:

public abstract class person {     public abstract void showinfo(); }  public class teacher : person {     public override void showinfo()     {         console.writeline("i teacher!");     } }  public class student : person {     public override void showinfo()     {         console.writeline("i student!");     } } 

if called, behavior of showinfo varies, based on implementation:

person person = new teacher(); person.showinfo();    // shows 'i teacher!'  person = new student(); person.showinfo();    // shows 'i student!' 

both, students , teachers persons, behave different when asked prompt information themselves. however, way ask them prompt information, same: using person class interface.

so happens behind scenes, when inherit person? when implementing showinfo, pointer not pointing nowhere longer, points actual implementation! when creating student instance, points students showinfo:

illustration of inherited methods

2. virtual methods

the second way use virtual methods. behavior same, except providing optional default implementation in base class. classes virtual members can instanciated, inherited classes can provide different implementations. here's code should work:

public class person {     public virtual void showinfo()     {         console.writeline("i person!");     } }  public class teacher : person {     public override void showinfo()     {         console.writeline("i teacher!");     } } 

the key difference is, base member person.showinfo isn't pointing nowhere longer. reason, why can create instances of person (and not need marked abstract longer):

illustration of virtual member inside base class

you should notice, doesn't different first image now. because virtual method pointing implementation "the standard way". using virtual, can tell persons, can (not must) provide different implementation showinfo. if provide different implementation (using override), did teacher above, image same abstract. imagine, did not provide custom implementation students:

public class student : person { } 

the code called this:

person person = new teacher(); person.showinfo();    // shows 'i teacher!'  person = new student(); person.showinfo();    // shows 'i person!' 

and image student this:

illustration of default implementation of method, using virtual-keyword

3. magic `new` keyword aka "shadowing"

new more hack around this. can provide methods in generalized classes, have same names methods in base class/interface. both point own, custom implementation:

illustration of "way around" using new-keyword

the implementation looks one, provided. behavior differs, based on way access method:

teacher teacher = new teacher(); person person = (person)teacher;  teacher.showinfo();    // prints 'i teacher!' person.showinfo();     // prints 'i person!' 

this behavior can wanted, in case misleading.

i hope makes things clearer understand you!


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -