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?
Thanks a lot! nbarraille
Upcasting is assigning the sub class reference object to the parent class which is allowed in Java. Downcasting is assigning parent class reference object to the sub class which is not allowed in Java. However, if you do downcasting, there will not be any compiler error.
What are Upcasting and Downcasting in Java? Upcasting (Generalization or Widening) is casting to a parent type in simple words casting individual type to one common type is called upcasting while downcasting (specialization or narrowing) is casting to a child type or casting common type to individual type.
Downcasting is not allowed without an explicit type cast. The reason for this restriction is that the is-a relationship is not, in most of the cases, symmetric. A derived class could add new data members, and the class member functions that used these data members wouldn't apply to the base class.
In class-based programming, downcasting or type refinement is the act of casting a reference of a base class to one of its derived classes.
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:
interface AnimalFactory { Animal createAnimal(); } class DogFactory implements AnimalFactory { public Dog createAnimal() { return new Dog(); } }
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 a Dog
, it still behaves like a Dog
.
You should only cast to a class that the object really is, so if you have a Dog
that extends Animal
you can cast it to an Animal
(because it is one) but you shouldn't cast an Animal
to a Dog
because not all Animal
s are Dog
s. The Dog
class may well have extra fields that are not implemented by the Animal
class and so the cast doesn't make sense (what do you initialise those values to?).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With