I have an interesting situation that EMF forced me into:
abstract class AbstractDog{
...
}
public class Dog extends AbstractDog{
< implemented code >
}
public class DogTemplate extends AbstractDog{
< implemented code identical to Dog >
}
The code found in both Dog and DogTemplate are literally identical (don’t ask – our system is very reliant on Eclipse stuff and this is the only way). I need to cast from a DogTemplate into a Dog, but getting java.lang.ClassCastException when I do Dog d = (Dog) dogTemplateInstance. I completely understand why this exception is occurring, so please don’t bother telling me why.
What I need to know is if there is any way to leverage the common ancestor relationship between the 2 classes to make this cast happen?
There is no way a
DogTemplatecan be cast to aDog, sinceDogdoesn’t even extendDogTemplate. Only a Dog instance can be cast to a Dog.You could create a new Dog instance using a constructor which would copy all the fields of DogTemplate to Dog, if it’s possible. Or you could use a common interface to both classes.