Beks
Beks

Reputation: 13

Bound mismatch with Generic Types

After one day searching I decided to ask your helps guys :-) here is my issue: I need to write coverter for some pojos.

public abstract class Answer<T extends Serializable> implements Serializable {//some code}

public class BooleanAnswer extends Answer<Boolean> {//some code}

public abstract class AnswerDMO<T extends Serializable> implements Serializable {//some code}

public class BooleanAnswerDMO extends AnswerDMO<Boolean>  {//some code} 

public interface Converter<I, O> {
O convert(I input);
}

public abstract class AnswerConverter<A extends Answer<Serializable>, J extends AnswerDMO<Serializable>>
                                        implements Converter<A, J>, Serializable {
@Override
public J convert(A input) {
    // some code
}
}

public class BooleanAnswerConverter extends AnswerConverter<BooleanAnswer, BooleanAnswerDMO>
{
    @Override
    public BooleanAnswerDMO convert(BooleanAnswer input) {
        // some code
    }
}

I'm getting an error on the BooleanAnswerConverter, the parameter BooleanAnswer is not within its bound, should extend Answer I tried many combination but couldnt get it right.

How to fix it?

Upvotes: 1

Views: 104

Answers (2)

Deividi Cavarzan
Deividi Cavarzan

Reputation: 10110

Since A and J extends Answer and AnswerDMO, and they have the type, you must change the AnswerConverter from:

public abstract class AnswerConverter<A extends Answer<Serializable>, J extends AnswerDMO<Serializable>>

To:

public abstract class AnswerConverter<A extends Answer, J extends AnswerDMO>

Answer and AnswerDMO has the type that is forcing the Serializable. Boolean in you example. Answer<Serializable> will try to ensure that the final implementation is this one, not a generic Answer.

This change result in correct compile of class:

public class BooleanAnswerConverter extends AnswerConverter<BooleanAnswer, BooleanAnswerDMO> {
    @Override
    public BooleanAnswerDMO convert(BooleanAnswer input) {
        return null;
    }
}

Upvotes: 1

Michael Butscher
Michael Butscher

Reputation: 10969

I don't know if this is what you want but a working solution would be

abstract class AnswerConverter<A extends Answer<? extends Serializable>,
        J extends AnswerDMO<? extends Serializable>> implements Converter<A, J>, Serializable {

Upvotes: 0

Related Questions