I am trying to understand Java's polymorphism, and I have one question about downcasting an object. Let's say for this example I have two subclasses Dog and Cat that inherit from a superclass Animal
From what I understood, the only way to downcast an object is if this Object is already of the good type, like this:
Animal a = new Dog();
Dog d = (Dog) a;
This works right?
But what if I want to create a regular animal without knowing what it would be, and then cast it when I know, how can I do that?
Animal a = new Animal();
Dog d = (Dog) a;
This will throw a ClassCastException at runtime right?
The only way I found to do that is to create a new Dog constructor that creates a dog from a regular animal:
Animal a = new Animal();
Dog d = new Dog(a);
with
public Class Dog extends Animal{
public Dog(Animal a){
super(a);
}
}
So my question is, how am I supposed to do this?
- Am I doing it the best way?
- Am I not supposed to do this at all, if I have to it means my program is not well conceived?
- Is there a better way I missed?
Thanks a lot! nbarraille
If you want to create an instance of a type that may vary depending upon non-local conditions, use an Abstract Factory (as described in the Design Patterns book).
In it's simplest form:
Note also there is a difference between the static type of a reference and the dynamic type of the object. Even though you have an
Animal
reference, if the original object is aDog
, it still behaves like aDog
.Here you are talking about downcasting, so in this scenario always superclass should be used as a reference and child object should be pointed by that. This usd basically in factory patter.
You should only cast to a class that the object really is, so if you have a
Dog
that extendsAnimal
you can cast it to anAnimal
(because it is one) but you shouldn't cast anAnimal
to aDog
because not allAnimal
s areDog
s. TheDog
class may well have extra fields that are not implemented by theAnimal
class and so the cast doesn't make sense (what do you initialise those values to?).Java is a strongly typed language, and that means you can only cast an object to a type it extends from (either a superclass or an interface).
Even if you "fake it", e.g. copy all a classes methods and fields, you just can't cast an object to a type it doesn't extend.
Given the above code, you can't cast a
Foo
object to either aBar
or aBaz
, although the class structure seems to imply that you could. There is no inheritance involved, so aClassCastException
is thrown.