I've these two Java interfaces:
the first one is:
public abstract interface A {
}
and the second one is:
public abstract interface B {
public abstract Set<A> methodName();
}
Then, I've implemented these two interfaces as:
public class AImpl implements A {
}
and:
public class BImpl implements B {
private Set<AImpl> p;
public Set<A> methodName() {
return this.p;
}
}
I don't understand why I obtain the following error about the implementation of methodName():
Type mismatch: cannot convert from Set<AImpl> to Set<A>
Thank you very much.
Set<AImpl>
is not exactly the same what Set<A>
, you cannot convert it.
You can:
Set<A>
Set<? extends A>
Set<AImpl>
in methodName()
More details:
if AImpl
implements/extends A then List<AImpl>
does not implement/extend List<A>
.
List<? extends A>
means that this is the list of something that extends/implements A.
Look at Wildcards and subtyping in Java Tutorial
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