Results 1 to 8 of 8
  1. #1
    Chronoattica is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default Storing Mutiple Classes/Data Types in a Linked List

    Is the following possible?

    I have 2 classes that extend a parent class;

    Animal - the parent class
    Dog - extends animal
    Cat - extends animal

    Now from another class in the package I create a linked list, say myanimals, and I want to store both Dog and Cat in myanimals.

    I then want to iterate through the list and if the type execute a method that is present in both of the Dog and Cat classes.

    Is it possible to do this or does a list only contain one data type? I have been playing with attempting to do this and I can not seem to find a way to do it.

    Thanks
    Andy

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    yes its possible : List<Animal> myanimals = new LinkedList<Animal>();

  3. #3
    Chronoattica is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    How do you add items say from Cat or Dog?

    I have tried

    Cat temp = new Cat(2000); // create new object
    myanimals.add(temp);

    and it gives the following error?

    The method add(Animal) in the type LinkedList<Animal> is not applicable for the arguments (Cat)

    How should I go about adding the item to the list?

    Thanks
    Andy

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    ?? Thats correct. Are you sure, that your Cat class extends the Animal class?

    class Cat extends Animal ??

  5. #5
    Chronoattica is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    Ahhh :)

    I thought I had added "Extends Animal" to the end of the class definition when clearly I had not.

    Thanks, that works.

    Now how would you go about checking if it was a Dog or a Cat Class? Use status flags or is there a way to get the class type name?

  6. #6
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Yeah, make sure that Cat extends Animal and Dog extends Animal.

    And if thats the case then Cat and/or Dog should get inside the LinkedList myanimals.

    Because here, polymorphism gets into the picture and we can pass subclass object where superclass object is expected.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  7. #7
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

  8. #8
    Chronoattica is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks, I just discovered that one myself a moment or two ago :)

    I started learning Java a few months back and are getting back into it and i'm jumping all over the shop learning what I need to know, using Sams Teach yourself Java, googling for stuff as well, and getting rather confused at times as well. :)

    Thanks again!
    All good fun. :)

Similar Threads

  1. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  2. Replies: 1
    Last Post: 12-03-2009, 07:03 AM
  3. Replies: 0
    Last Post: 03-31-2009, 04:52 PM
  4. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 04:19 AM
  5. Doubly-linked list with data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •