2

I have a few ArrayList of objects that implement an interface and I want to pass them to a function that deals with the interface.

The problem is I can't pass an ArrayList of the object to a function that accepts an ArrayList of the interface

Edit: Functions that use ArrayList of the object will not be able to accept an ArrayList of the interface. Can this be done without creating two lists?

Edit: Question marked as duplicate. I understand the issue however the previous question doesn't state if there is a solution for passing a list of object to a list of interfaces.

A simplified version of my problem below

    //Interface
public interface SomethingInterface{
    abstract void doesntmatter();
}
//Object
public class SomethingObject implements SomethingInterface{
    public void doesntmatter(){}
}
//Second Object
public class SomethingObject2 implements SomethingInterface{
    public void doesntmatter(){}
}
//Function that wants to be able to accept all objects that implement the interface
public void PassToInterface(ArrayList<SomethingInterface> list){
    for(SomethingInterface i : list){
        i.doesntmatter();
    }
}
//Function that uses list of one of the objects
public void PassToObject(ArrayList<SomethingObject> list){
    //Do something
}

public static void main(String args[]){
    ArrayList<SomethingObject> somethings = new ArrayList();
    ArrayList<SomethingObject2> somethings2 = new ArrayList();

    //Unable to pass ArrayLists of the objects that implement the interface
    PassTo(somethings);
    PassTo(somethings2);
}   
TheOnlyWAR10CK93
  • 113
  • 1
  • 10
  • If you read the answers in the duplicate carefully, they explain that you can use wildcards to achieve what you want. – Sotirios Delimanolis Feb 23 '15 at 17:50
  • Yes the keyword was 'extends' which obviously would work for Super classes but the keyword for interfaces is 'implements' so I was confused and unsure if people had read my question properly if the fix was so simple. I have now put in ArrayList extends SomethingInterface> and it works. Very simple fix. – TheOnlyWAR10CK93 Feb 23 '15 at 22:28
  • http://stackoverflow.com/questions/976441/java-generics-why-is-extends-t-allowed-but-not-implements-t?lq=1 – Sotirios Delimanolis Feb 23 '15 at 22:29

1 Answers1

0

Your PassTo method receive ArrayList<SomethingInterface> as argument.

You need to specify somethings and somethings2 to accept SomethingInterface but you will still be able to add object SomethingObject and SomethingObject2 in it.

ArrayList<SomethingInterface> somethings = new ArrayList();
ArrayList<SomethingInterface> somethings2 = new ArrayList();

somethings.add(something);
somethings2.add(something2);

PassTo(somethings);
PassTo(something2);
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
  • Yes that would work for this example however assuming I already have ArrayList. Now any functions that accept ArrayList cannot accept ArrayList – TheOnlyWAR10CK93 Feb 23 '15 at 17:43
  • I don't see why you would have a function that accept SomethingObject in parameter, but not SomethingObject2 as they are both child of SomethingInterface. If a comportment is specific to one, then it should be in the SomethingObject class. – Jean-François Savard Feb 23 '15 at 17:46
  • This is very a simple example illustrating my problem. In my actual code I have a problem where the parent needs to deal with a list of the two objects separately as they are both different however share the same interface. I'm hoping for a way to be able to pass between an ArrayList to ArrayList or be told it's not possible. Trust me in other words. A function that is applied to a list of one object is not applicable to the other. However a function applied to a list of the interface of both objects is applicable for both. – TheOnlyWAR10CK93 Feb 23 '15 at 17:58
  • It is not possible without using wildcard, it is illogical. See this post http://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-arent-javas-generics-implicitly-p – Jean-François Savard Feb 23 '15 at 18:04
  • In the case of pointing a child list to a list of type parent, the child list has restrictions the parent does not thusly problems. However in the case of passing a child list to a function that deals with a parent list, the function can only use methods of the parent thusly all children will work with that function. I understand that java works by pointing between functions, just as a function such as this would work I thought java would accommodate for the parents hierarchy to take over. Anyway I will check out wildcards, thank you. – TheOnlyWAR10CK93 Feb 23 '15 at 18:41