Examine the code below
public abstract class ClassA<ClassBType extends ClassB<ClassCType>,ClassCType extends ClassC> {
public void method(ClassBType type) {
type.myClassA = this; //Error. Type mismatch: cannot convert from TestGameMain.ClassA<ClassBType,ClassCType> to TestGameMain.ClassA<TestGameMain.ClassB<ClassCType>,ClassCType>
}
}
public abstract class ClassB<ClassCType extends ClassC> {
ClassA<ClassB<ClassCType>,ClassCType> myClassA;
private void testMethod() {
myClassA.method(this);
}
}
public abstract class ClassC {}
}
What’s the correct way to fix this problem?
Edit: I’ve updated the code above, which does not compile.
Your example is overly complicated – you can demonstrate the same issue even without
ClassC:The problem comes down to variance: there is no inheritance relationship between a
ClassA<ClassB>and aClassA<ClassBType>(nor should there be), so the assignment can’t be made. Given the cryptic nature of this example, I’m not sure if this is actually a “solution” to your problem, but the following code does compile: