Why not do the reverse? Instead of super.cast(sub)
do sub.copy(super)
Example:
class Sub1 extends Super {
public Super copy(Super input) {
return new Sub1(input);
}
}
class Sub2 extends Super {
public Super copy(Super input) {
return new Sub2(input);
}
}
etc.
This is called in nearly the same way and prevents having any if statements, and gets exactly the same functionality.
If your old code was:
void doSomething(Super super, Super iAmActuallyASub) {
Super superDuper = super.cast(iAmActuallyASub);
}
The new code would be
void doSomething(Super super, Super iAmActuallyASub) {
Super superDuper = iAmActuallyASub.copy(super);
}