0

I have a hierarchy of classes that have behaviour classes (classes with a reference to its parent) following the same hierarchy. The reference in the behaviour classes is a generic reference in order to avoid stacking up interfaces.

In the middle of this class hierarchy, a generic Map of data is introduced too, and this causes the following problem for me...

public static class A {}
public static class B<DataType> extends A {}

public static class C<ParentType extends A> {}
public static class D<DataType, ParentType extends B<DataType>> extends C<ParentType> {}

C<A> castDtoC() { return new D<Long, B<Long>>(); } // Doesn't compile
A    castBtoA() { return new B<Long>(); } // Works fine

I don't understand why it cannot cast, is there a way to fix this?

I know I could avoid the reference by making the behaviour classes into inner classes, but then I will have 1000+ lines of code per file, and I'd like to avoid having to cast the reference each time I use it the children classes.

4castle
  • 32,613
  • 11
  • 69
  • 106

0 Answers0