3
interface Message<T, L> {
}

interface Foo<T> {
    void frob(Message<T, Foo<?>> message);
}

class AuxiliaryFoo implements Foo<Integer> {
    @Override
    public void frob(Message<Integer, Foo<?>> message) { }
}

class MainFoo implements Foo<Object> {
    @Override
    public void frob(Message<Object, Foo<?>> message) {
        new AuxiliaryFoo().frob(new Message<Integer, MainFoo>() {});
    }
}

The Java compiler tells me that actual argument < anonymous Message< Integer, MainFoo >> cannot be converted to Message< Integer, Foo< ?>> by method invocation conversion.

Why?

And what can be converted to Message< Integer, Foo< ?>> ?

reevesy
  • 3,452
  • 1
  • 26
  • 23
Joker_vD
  • 3,715
  • 1
  • 28
  • 42

1 Answers1

10

You should use <? extends Foo<?>>

interface Foo<T> {     
    void frob(Message<T, ? extends Foo<?>> message); 
} 

Also, something worthy to keep in mind when dealing with generics is the PECS rule: Producer Extends, Consumer Super though it doesn't directly belong here, but I can't say that it doesn't belong here at all..

Community
  • 1
  • 1
ppeterka
  • 20,583
  • 6
  • 63
  • 78