3

Is there a way that I can pass a generic type to a generic class like

interface Wrapper<T, R> {}

interface Result<T, Wrapper<T, R>> {
    R getResult();
}

Here Result takes Wrapper one of the type with type arguments T and R. getResult method in Result interface returns R.

What I did is below,

interface Result<T, W> {
    <K> K getResult();
}

Is there a better way to do this in java.

1
  • Wrapper in interface Result<T, Wrapper<T, R> is not using your existing Wrapper<T, R> interface. It's hiding it by declaring a new type parameter of the same name. Commented Nov 21, 2019 at 6:00

1 Answer 1

4

You need a separate type argument for a Result interface. Something like this:

interface Result<T, R, W extends Wrapper<T, R>> {
    R getResult();
}

Now you can use W inside the result to represent the Wrapper or T and R to represent wrapper components. It's possible though that W is completely unnecessary for you and having T and R is enough.

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

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.