I have an interface like so
public interface A {}
and an interface which extends from the previous like so
public interface B extends A {}
The constructor of one of my classes takes as parameter a Function<A, String>
object. Within the constructor, several objects which implement A
are passed into said function to generate a string.
I would like to be able to pass this constructor a Function<B, String>
object, as all classes that implement B
implement A
by extension. However, when attempting to do this, I am told "Incompatible types: A is not convertible to B". Which I don't understand as I am trying to convert B
to A
not A
to B
I have tried changing the parameter type from a Function<A, String>
to a
Function<? extends A, String>
but alas this doesn't work either.
Is there some way I can achieve passing either a Function<A, String>
or a Function<B, String>
into the same argument?
I am creating a game using the Swing library, and my A
is Displayable
and B
is Purchasable
. I have a class DisplayablePanel
which extends JPanel
and creates a visual representation of any displayable object in my game. All Purchasables
are Displayable
so I can display them using my DisplayPanel
however, I want Purchasables
to be able to display Purchasable
specific information too. Here my constructor uses the Function<A, String>
to decide what text should be displayed on a button that sits on the DisplayPanel
And for my Purchasable
s this information is specific to the Purchasable
interface