1

I saw something similar to this using inheritance but I want to use an interface I want to create a single arraylist consisting of different objects. ArrayList containing the following objects |Duck, Duck, Goose, Duck, Goose, Pig| I can just add random ducks and geese. i want to do this so for example lets say I want to eat a duck and once im done with the duck i can eat another duck and iterate through each item and run an "eat" method for example
|Could I do the following:

public interface Animal {..}

public class Duck implements Animal(){}

public class Goose implements Animal{
     List<Animal> link = new ArrayList<Animal>;

Goose(Duck d){
     link.add(d);
 }
Goose(Goose g){
     link.add(g);
 }
 Goose g3 = new Goose(new Duck);
 Goose g2 = new Goose(g2);
}

`

ssedano
  • 8,322
  • 9
  • 60
  • 98
Met met
  • 25
  • 1
  • 2
  • 4
    I find it difficult to understand what your question is. It would also help potential answerers out if your source code were formatted and didn't have syntax errors (e.g. `...new ArrayList;` should be `...new ArrayList();` with the parentheses at the end). – Nick May 21 '13 at 22:11
  • 1
    I think the answer is "yes." You can use an interface as a type parameter. Generally speaking, the best way to answer this kind of question is just to try it yourself. – jpmc26 May 21 '13 at 22:38
  • You're missing a lot of code that might help clarify. As Nick suggested clean it up and complete it and perhaps we can help. Your code does not seem to match your question. In essence "Could I do the follow:" would be answered with No as the following code does not eat any ducks nor does it remove any ducks from any lists. – galford13x May 21 '13 at 22:38
  • 1
    Yes, you can define an interface named _Animal_ that allows you to declare an ArrayList that contains any object that implements _Animal_. However, the syntax in your code is incorrect. – jahroy May 21 '13 at 22:39
  • Here's [an answer](http://stackoverflow.com/a/12684564/778118) that is somewhat related to what you want to do. – jahroy May 21 '13 at 22:40

2 Answers2

1

Your code makes absolutely no sense to me. I think I understand what it is you want though. First let's go through your code.

You have a Goose class which implement Animal which is good. Then you have a list called link in the class:

public class Goose implements Animal{
    List<Animal> link = new ArrayList<Animal>(); //remember ().
    ...

Each instance of your Goose class will have a list of animals. This looks odd. From the question though, it sounds like you want 1 list which contain animals and not a list within each Goose.

Then you have two constructors:

Goose(Duck d){
    link.add(d);
}
Goose(Goose g){
    link.add(g);
}

You require either a Duck or a Goose as input which you add to the internal list of the new Goose. Meaning each new instance will start out with either a Duck or a Goose inside its list. It seems like you want your constructors to be add methods.

Finally you have these:

Goose g3 = new Goose(new Duck()); //You were missing () here also
Goose g2 = new Goose(g2);         //Syntax error. Should probably be ".. new Goose(g3);"

I am not entirely sure why you have these inside your Goose class and they look like they are supposed to be declared elsewhere. As it is now each Goose contain two other geese called g3 and g2 which does not make much sense either (except if they were used as reference to the parents of the goose maybe, but it does not seem to be the case).

What I think you want and how you achieve it: You need to modify your Goose class so each goose only have members relevant for each goose. It could look like this (assuming the animal interface declare an eat() method):

public class Goose implements Animal {
    @Override
    public void eat() {
        // Om nom nom
    }
}

You could then move link, g2 and g3 to the main method:

public static void main(String[] args) {
    List<Animal> link = new ArrayList<Animal>();
    Goose g2 = new Goose();
    Goose g3 = new Goose();

    link.add(g2);
    link.add(g3);
    ..

Doing this you can only use the link list in the main method. You could also make it a property of the class containing main to broaden the scope. If you need global access you could make a singleton class which contain it (or make it public static somewhere, but I would recommend another approach).

You can also add a Duck to the list if the Duck class implements the Animal interface correctly:

link.add(new Duck());

If the Animal interface declares a public method called eat you can now iterate through the list and make all the animals eat:

    for(Animal a : link) {
         a.eat();
    }

Feel free to comment if I missed something.

MAV
  • 7,260
  • 4
  • 30
  • 47
  • Helps, thank you. sorry for the errors I want to basically create rows of ducks and geeses automatically through an input. User inputs numRows & numCols. Each row/column will be filled randomly with goose and duck objects in an array list so I can iterate through each animal one by one. Once you eat the first animal, you go to the second.. SO each row and column are iterating throw its array list at the same time – Met met May 22 '13 at 15:30
0

Your code has a few errors. For the Goose part you can do something like this:

public class Goose implements Animal {
List<Animal> link = new ArrayList<Animal>();

Goose(Duck d){
     link.add(d);
 }
Goose(Goose g){
     link.add(g);
 }

public static void main(String... args){
    Goose g3 = new Goose(new Duck());
    Goose g2 = new Goose(g3);
}

}

It means that if you have interface Animal you can create a list of objects of type Animal and fill it with any object which is instance of Animal (it's class extends Animal interface).

Is that what you wanted to know?

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Correct. Thank you. I want to basically create rows of ducks and geeses automatically through an input. User inputs numRows & numCols. Each row will be filled randomly with goose and duck objects in an array list so I can move through each animal one by one. Once you eat the first animal, you go to the second.... – Met met May 22 '13 at 15:29