I'm developing a Java library and I need to create a method which returns a Set<A>
, where A
is an interface and B implements A
. So I tried instantiating the set like:
Set<A> setOfA = new HashSet<B>();
getting the following error:
Type mismatch: cannot convert from HashSet<B> to Set<A>
However, when not using a collection and returning B
in a function which expects A
as return type everything is ok, so the relation between the interface and its concrete class is ok and the problem is with collections, the HashSet in this case. How can I avoid this? Thanks in advance.