3

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>

0

3 Answers 3

9

Use following:

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

See Generic Methods for more details.

Sign up to request clarification or add additional context in comments.

Comments

1

Unless you actually need a type variable in the method body, you can simply use:

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

Comments

0

Wildcard is the way to go but you probably want some form of control over what type of object you are using.

Just make the custom classes (Object1, Object2, ...) implements a custom interface (CustomSuperInterface) and declare the collection like this

void onQueryCompleted(int token, List<? extends CustomSuperInterface> songList);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.