Are there any advantages in the advanced interface?

Recently, in an answer, I was asked the following:

public interface Operation<R extends OperationResult, P extends OperationParam> {

    public R execute(P param);
}

Better than this:

public interface Operation {

    public OperationResult execute(OperationParam param);
}

However, I see no benefit in using the first code over the second ...

Given that both OperationResultand OperationParamare interfaces, the developer needs to return a derived class, and it seems to me quite obvious.

So, do you see any reason to use the first code block over the second?

+3
source share
1 answer

This way you can declare your implementations Operationto return a more specific result, for example.

 class SumOperation implements Operation<SumResult, SumParam>

Whether or not this value matters to your application is entirely up to the situation.

: , , , .

+6

Source: https://habr.com/ru/post/1790912/


All Articles