Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Attempting to use an incompatible return type" with Interface Inheritance

I'm running into a problem with incompatible return types using inheritance.

public interface A { }
public interface B extends A { }

public interface C {
    Map<String, A> getMapping();
}

public interface D extends C {
    Map<String, B> getMapping();
}

Is there a way to make this work?

Right now the compiler tells me I'm 'Attempting to use an incompatible return type' on interface D.

like image 666
jjNford Avatar asked Jun 16 '15 21:06

jjNford


2 Answers

I suggest you use

interface C {
    Map<String, ? extends A> getMapping();
}

This says "A map that maps String to A or a subtype of A". This is compatible with Map<String, B>.

like image 124
aioobe Avatar answered Oct 19 '22 19:10

aioobe


Make the following changes:

interface C<E extends A> {
    Map<String, E> getMapping();
}

interface D extends C<B> {
    Map<String, B> getMapping();
}
like image 25
Luiggi Mendoza Avatar answered Oct 19 '22 17:10

Luiggi Mendoza