3

I have run into a problem with generics in Java and I can't find a solution or example online of someone using generics in a similar fashion. I have a set of methods that are in the same request/response structure. For example, a user populates fields in the Request object, passes the object to a helper method, and they are returned a Response object. All of my request objects extend from a common Request super class (and similarly, Response objects from a Response super class). I would like my helper classes to also have a consistent structure so I have used an interface. Here is some code to illustrate...

Request super class:

public class SuperRequest {
    // ...
}

Example Request subclass:

public class SubRequest extends SuperRequest {
    // ...
}

Response super class:

public class SuperResponse {
    // ...
}

Example response subclass:

public class SubResponse extends SuperResponse{
    // ...
}

The interface:

public interface SomeInterface {
    public <T extends SuperRequest, U extends SuperResponse> U someMethod(T request);
}

As you can see from the interface, I want to pass an object that is a child of SuperRequest and I want to return an object that is a child of SuperResponse. Here is my implementation:

public class SomeImplementation implements SomeInterface {

    @Override
    public SubResponse someMethod(SubRequest request) {
        return null;
    }
}

In Eclipse, I get compilation errors because the compiler tells me I have unimplemented methods and the @Override notation "does not override a supertype method".

Can anyone help me here? Is my syntax incorrect or is my understanding of generics a little off? Any help is greatly appreciated!

3 Answers 3

7

Try changing your interface and implementation to:

interface SomeInterface<T extends SuperRequest, U extends SuperResponse> {
    public U someMethod(T request);
}

class SomeImplementation implements SomeInterface<SubRequest, SubResponse> {

    @Override
    public SubResponse someMethod(SubRequest request) {
        return null;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I had a feeling I was just a bit off on the syntax/declaration. Thanks for the help!
1

As it stands, the implementation of SomeInterface must implement the parameterized method, i.e.:

public <T extends SuperRequest, U extends SuperResponse> U someMethod(T request);

It can't choose a particular Subclass to substitute for T and U.

The interface itself must be generic SomeInterface<T extends SuperRequest, U extends SuperResponse> and the subclass can then choose concrete implementations for T and U.

Comments

1

You have to declare the types as well when you implement the interface. Try this:

public class SomeImplementation implements SomeInterface<SubRequest,SubResponse> {

    @Override
    public SubResponse someMethod(SubRequest request) {
        return null;
    }
}

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.