crysis crysis - 6 months ago 25
Java Question

How to pass Object ArrayList through interface?

I've an interface defined as

public interface QueryCompleteListener {
void onQueryCompleted(int token, ArrayList<Object1> songList);
}


I'm using this interface to return callback. In some callbacks I want to pass
ArrayList<Object1>
and in some cases I want to pass
ArrayList<Object2>
through the interface.

If I declare interface method as


void onQueryCompleted(int token, ArrayList<Object> songList)



to pass any type of Object, it gives an error saying found Object1 required Object when I call this method by passing
ArrayList<Object1>

Answer

Use following:

<T> void onQueryCompleted(int token, List<T> songList); 

See Generic Methods for more details.