c# - Why can't I access methods specific to my child class? -


in c#, have parent class public member. want derive parent class, derive class of public member, create , access new methods, follows...

    public class animal { }     public class sheep : animal {         public void makealamb() { }     }     public class farm     {         public animal myanimal;     }     public class sheepfarm : farm {         public void sheepfarm() {             this.myanimal = new sheep();             this.myanimal.makealamb();         }     } 

this code doesn't compile. "animal not contain definition makealamb()". want essence of polymorphism, no? missing? i'm looking forward finding out.

thanks in advance!

if i'm guessing correctly you're intending do, consider using generics:

public class farm<tanimal> tanimal : animal {     public tanimal myanimal; }  public class sheepfarm : farm<sheep> {     public void sheepfarm()     {         this.myanimal = new sheep();         this.myanimal.makealamb();     } } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -