In my java code, I have an instance of a generic parent data structure being instantiated with a new instance of the generic child structure.
private ParentHand<ParentCard> hand;
public Foo() {
// Java: Incompatible types: ParentHand<ParentCard>
// cannot be converted to ChildHand<ChildCard>();
hand = new ChildHand<ChildCard>();
}
The parent interfaces are constructed as follows:
public interface ParentHand<T extends ParentCard> { ... }
public interface ParentCard extends Comparable<ParentCard> {...}
The child interfaces are constructed as follows:
public interface ChildHand<T extends ParentCard> implements ParentHand<T> { ... }
public interface ChildCard implements ParentCard { ... }
I believe the above should work because of polymorphism. This is written with Java 11. Why does Foo()
throw this error?