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

bbsimonbb

In C#, I have a parent class with a public member. I want to derive the parent class, then derive the class of the public member, so as to create and access new methods, as 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 does not contain a definition for makeALamb()". But what I want to do is the essence of polymorphism, no? What am I missing? I'm greatly looking forward to finding out.

Thanks in advance!

decPL

If I'm guessing correctly what you're intending to do, consider using generics:

public class Farm<TAnimal> where TAnimal : Animal
{
    public TAnimal myAnimal;
}

public class SheepFarm : Farm<Sheep>
{
    public void SheepFarm()
    {
        this.myAnimal = new Sheep();
        this.myAnimal.makeALamb();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I access my class instance methods?

From Dev

Can't access methods in my class

From Dev

configatron a singleton? Why can't I access the configatron in my class

From Dev

why can't I access the variable in my class. python

From Dev

Why I can't access my script variable from class?

From Dev

Why can I get access to static method of my super class from child instance?

From Dev

Why can't I access the utility methods in class members in T4 templates?

From Dev

I can't access my class property

From Dev

Why can't I access Child objects in my BindingSource in the Advanced Binding Dialog

From Dev

Why can't I access a member of this class?

From Dev

Why my class instance is of type int (I can't access its attributes)?

From Dev

Why can't I access the props directly in Child Component?

From Dev

Why can't I override my interface its methods?

From Dev

Why can't I call any methods in my code?

From Dev

Why can't I override my interface its methods?

From Dev

Proxy class can't call methods on child

From Dev

Why `this` can't access the derived class members from base class methods when called for derived class object

From Dev

How to debug why I can't access a specific website?

From Dev

I can't access to the attribute of my class with C++

From Dev

I Can't access my class library in a web application

From Dev

Why I can't set a specific address for my QTcpServer?

From Dev

Why can't I access my objects member variable?

From Dev

Why can't I connect to my access database

From Dev

Why can't I access my application delegate from a ViewController?

From Dev

Why can't I access my file in VS 2013 with TFS

From Dev

Why can't I access my vi folder?

From Dev

Why can't I access my activity with the package name?

From Dev

Why I can't access to a png image into a folder of my project?

From Dev

Why I can't access to my own User Control?

Related Related

  1. 1

    Why can't I access my class instance methods?

  2. 2

    Can't access methods in my class

  3. 3

    configatron a singleton? Why can't I access the configatron in my class

  4. 4

    why can't I access the variable in my class. python

  5. 5

    Why I can't access my script variable from class?

  6. 6

    Why can I get access to static method of my super class from child instance?

  7. 7

    Why can't I access the utility methods in class members in T4 templates?

  8. 8

    I can't access my class property

  9. 9

    Why can't I access Child objects in my BindingSource in the Advanced Binding Dialog

  10. 10

    Why can't I access a member of this class?

  11. 11

    Why my class instance is of type int (I can't access its attributes)?

  12. 12

    Why can't I access the props directly in Child Component?

  13. 13

    Why can't I override my interface its methods?

  14. 14

    Why can't I call any methods in my code?

  15. 15

    Why can't I override my interface its methods?

  16. 16

    Proxy class can't call methods on child

  17. 17

    Why `this` can't access the derived class members from base class methods when called for derived class object

  18. 18

    How to debug why I can't access a specific website?

  19. 19

    I can't access to the attribute of my class with C++

  20. 20

    I Can't access my class library in a web application

  21. 21

    Why I can't set a specific address for my QTcpServer?

  22. 22

    Why can't I access my objects member variable?

  23. 23

    Why can't I connect to my access database

  24. 24

    Why can't I access my application delegate from a ViewController?

  25. 25

    Why can't I access my file in VS 2013 with TFS

  26. 26

    Why can't I access my vi folder?

  27. 27

    Why can't I access my activity with the package name?

  28. 28

    Why I can't access to a png image into a folder of my project?

  29. 29

    Why I can't access to my own User Control?

HotTag

Archive