Interface generation causes a compilation error

Java 7

I had an interface:

public interface MyInt{
    public Map<String, WhereClause> createClauses(Parameters<Object> params);
}

and some of its implementations:

public class MyImpl implements MyInt{

    @Override
    public Map<String, WhereClause> createClauses(Parameters<Object> p) { //1
        //return some
    }
}

Now I could make its return type be generic. I tried this:

public interface MyInt <T extends SomeType>{
    public Map<String, ? extends WhereClause> createClauses(Parameters<Object> params);
}

But I got a compile-time error in the implementation in //1:

The method createClauses(Parameters<Object>) of type `MyImpl` must
override or implement a supertype method

But when I remove Generation, the implementation compiles fine.

Why generation influences compilation even though it is not used type parameter.

+4
source share
2 answers

In almost all cases when you use ?in generics, you do it wrong:

public interface MyInt<C extends WhereClause> {

    public Map<String, C> createClauses(Parameters<Object> params);
}

private static class MyWhereClause extends WhereClause {

    public MyWhereClause() {
    }
}

public class MyImpl implements MyInt<MyWhereClause> {

    @Override
    public Map<String, MyWhereClause> createClauses(Parameters<Object> p) {
        return null;
    }
}
+5
source

, , , , , .

: - , . , " ". , ; API, Collection, . (- "" , )

10 . , (.. ), . ( ), , . , , .

. - ( ). .

, interface MyInt2<T> extends MyInt, MyInt .

+1

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


All Articles