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);
}