Method for generating Java generics

I have an interface:

public interface CartService extends RemoteService{
    <T extends ActionResponse> T execute(Action<T> action);
}

      

I want to override the execute method:

public class XX implements CartService {

  @Override
  public <GetCartResponse> GetCartResponse execute(GetCart action) {
    // TODO Auto-generated method stub
    return null;
  }
}

      

But get compiler error: Execution of method (GetCart) of type XX must override or implement method of supertype

GetCart and GetCartResponse:

public class GetCart implements Action<GetCartResponse>{

}


public class GetCartResponse implements ActionResponse {
    private final ArrayList<CartItemRow> items;

    public GetCartResponse(ArrayList<CartItemRow> items) {
        this.items = items;
    }

    public ArrayList<CartItemRow> getItems() {
        return items;
    }

}

      

How can I override this method?

+2


a source to share


3 answers


The problem is defining an interface with what erausre should look like for your impl. In your example, you have:

<T extends ActionResponse> T execute(Action<T> action);
public <GetCartResponse> GetCartResponse execute(GetCart action);

      

but according to the definition of the interface, the implementation method should read:

public GetCartResponse execute(Action<GetCartResponse> action);

      



So I think you need to either change the signature of your interface or add another type parameter like:

public interface CartService extends RemoteService{
    <T extends ActionResponse, U extends Action> T execute(U action);
}

      

or perhaps something like lines:

public interface CartService extends RemoteService{
    <T extends ActionItem> ActionResponse<T> execute(Action<T> action);
}

      

+2


a source


Your interface states that the implementation classes should define this method for everyone T extends ActionResponse

. You want to define them for specific actions and responses - I think your interface should be

    public interface CartService<T extends ActionResponse, A extends Action<T>>
     extends RemoteService {
      public T execute(A action)
    }

      

and then the implementation will be

public class XX implements CartService<GetCartResponse,GetCart> {
 //...
}

      



As written, you may not need to explicitly parameterize an extension with an extension Action

- if you are dealing with any type Action

parameterized GetCartResponse

and do not rely on the specifics of the action GetCart

. In this case, your interface should look something like this:

public interface CartService<T extends ActionResponse> extends RemoteService {
 public T execute(Action<T> action);
}

      

and implementation

public class XX implements CartService<GetCartResponse> {
 public GetCartResponse execute(Action<GetCartResponse> action) {
  //...
 }
}

      

+1


a source


Use

public <GetCartResponse extends ActionResponse>
    GetCartResponse execute(GetCart action) {
    //do stuff
}

      

as a signature for the execute method in CartService

, it should work.

I have verified that the follwing; just changed ActionResponse

to Integer

and Action

to List

for convenience.

CartService:

public interface CartService {
    <T extends Integer> T execute(List<T> action);
}

      

XX:

public class XX implements CartService {
    public <T extends Integer> T execute(List<T> action) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

      

0


a source







All Articles