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.
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>
.
Make the following changes:
interface C<E extends A> {
Map<String, E> getMapping();
}
interface D extends C<B> {
Map<String, B> getMapping();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With