0

I am trying to implement a system as follows:

DtoMapper: This is an interface which defines a contract any third party mappers should return objects as

    public interface DtoMapper<T> {

    /**
     * Converts list of T into FooResponse
     * @param List of T
     * @return FooResponse
     */
    FooResponse convertToFooResponse(final List<T> rewards);

FooMapper: implements DtoMapper passing a List of Bar as method argument, however the compiler does not like my override of the method implementation and is wanting the second implementation (bottom).

public final class FooMapper implements DtoMapper {


    @Override
    FooResponse convertToFooResponse(final List<Bar> listOfBars) {
        ... Logic
    }

     @Override
    public convertToFooResponse(final List listOfBars) {
        ... I dont want this
    }

How can I make my interface work with this requirement, so that in future another implementation of say public convertToFooResponse(final list<Snafu> listOfSnafus); ?

2
  • Can you show the declaration of FooMapper? Commented Sep 8, 2016 at 11:06
  • Updated the class declaration for you Commented Sep 8, 2016 at 11:07

4 Answers 4

5

You need to explicitely implement DtoMapper<Bar> instead of DtoMapper.

Example:

public interface DtoMapper<T> {

    /**
    * Converts list of T into FooResponse
    * @param List of T
    * @return FooResponse
    */
    FooResponse convertToFooResponse(final List<T> rewards);
}

public class FooMapperImplementation implements DtoMapper<Bar> {
    @Override
    public FooResponse convertToFooResponse(List<Bar> rewards) {
        return null;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

You need to pass the generic specification when implementing the class:

public final class FooMapper implements DtoMapper<Bar> {
// Here -----------------------------------------^

    @Override
    FooResponse convertToFooResponse(final List<Bar> listOfBars) {
        ... Logic
    }

2 Comments

This answer is not adding anything new not provided in stackoverflow.com/a/39389316/6783451 which was posted before yours.
If think @JoseIgnacioAcinPozo is right. And he also added more information.
1

You forgot the parameterized type

class FooResponse{}
class First{}
class Second{}

public interface DtoMapper<T> {
    FooResponse convertToFooResponse(final List<T> rewards);    
}

class FirstMapper implements DtoMapper<First> {
    @Override
    public FooResponse convertToFooResponse(List<First> list) {
        return null;
    }
}

class SecondMapper implements DtoMapper<Second> {
    @Override
    public FooResponse convertToFooResponse(List<Second> list) {
        return null;
    }
}

Comments

0

Before calling the method, you can create a new list with interface type.

List<InterfaceType> listWithInterfaceType = new ArrayList<>(listWithObjectType);

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.