1

I've used and extended classes where the name looks like ArrayAdapter<String> or HashMap<String, String>, but how can implement the type inside the <> myself? For example, I have a class that is currently used to store callback data. It looks like:

static class APICallback {
    boolean status;
    JsonObject data;
}

Is there a way for me to implement this so that I can code something like APICallback<List<String>> and have it know the data field will be a List<String> vs a JsonObject?

1

2 Answers 2

3

Your class would have to be a generic type:

E.g:

static class APICallback<T> {
    boolean status;
    T data;
}
Sign up to request clarification or add additional context in comments.

Comments

2

If I understand your question, you want to know about generic types (from the link) a generic type is a generic class or interface that is parameterized over types. One possible way to make your class generic would be

static class APICallback<T> {
    boolean status;
    JsonObject data;
    List<T> al;
}

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.