How do I instantiate an object of a derived class using the parent class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fuzz13
    New Member
    • Jun 2010
    • 110

    #1

    How do I instantiate an object of a derived class using the parent class?

    I'm trying to instantiate an object of an inherited class "Chimp" from the parent class "Animal". The Animal class has a constructor that allows for an int legcount, string skintype, string talk, and a string eats. I have several objects instantiated as:
    Code:
    Squirrel squeeky = new Squirrel(FOUR_LEGS, "String1", "String2", "String3");
    Chicken cluck = new Chicken(TWO_LEGS, "String1", "String2", "String3");
    I am trying to instantiate my Chimp as an Animal though and have something like:
    Code:
    Chimp bubbles = new Animal(FOUR_LEGS, "String1", "String2", "String3");
    But that is wrong. It returns an error saying it can't implicitly convert Animal to Chimp. When I set it as:
    Code:
    Animal bubbles = new Chimp(FOUR_LEGS, "String1", "String2", "String3");
    The instantiation works but my Chimp.Unique() Method doesn't work (a method within the chimp class unique to the chimp class).

    Can someone please explain if both of those are wrong or how I do it using one or the other but can still reference the unique methods in the class?

    Thanks in advance.
  • Sfreak
    New Member
    • Mar 2010
    • 64

    #2
    When you instantiate a class, all parents are instantiate together. To access an attribute of a parent use the BASE statement to get access of it.

    Comment

    • Fuzz13
      New Member
      • Jun 2010
      • 110

      #3
      I understand that but I'm being asked to instantiate it in the specific way I'm having trouble doing. Can you help with that?

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        I think we'd need to see your inheritance structure... can you post a class diagram possibly? If Chimp inherits from Animal, you should be able to assign a new Animal to a Chimp object.

        A class diagram or some sample code so I can see what you're doing would help.

        Comment

        • Fuzz13
          New Member
          • Jun 2010
          • 110

          #5
          Thank you for your replies. What I ended up needing was:
          Code:
          Animal bubbles = new Chimp(FOUR_LEGS, "String1", "String2", "String3");
          The problem it was giving me that I didn't understand was that it broke the connection with my method

          bubbles.dosomet hingunique();

          As it turns out since I instantiate it as a member of the parent class, it had the attributes of that class and not of the Chimp class like it would have had if I instantiated it as:
          Code:
          Chimp bubbles = new Chimp(FOUR_LEGS, "String1", "String2", "String3");
          Anyways that error was giving me serious confusion until it was explained to me. Thank you for your assistance though guys.

          Comment

          Working...