General Question

nicky's avatar

Can someone explain the concept of downcasting in java?

Asked by nicky (207points) April 14th, 2010
5 responses
“Great Question” (1points)

I have a test tomorrow in my Java CIS class and i was hoping someone would be able to explain the concept of downcasting. I know you use it when you have subclasses of subclasses (the particular subclass inherits indirectly from the superclass,) but the reason why im a little unclear on (my teacher really likes to make us explain concepts in short answers)

Observing members: 0
Composing members: 0

Answers

timtrueman's avatar

Bruce Eckel’s Thinking in Java, a freely-available book (you can download the whole thing from his site), is excellent and his description and example of downcasting can probably explain the concept better than I can explain it…

nicky's avatar

so if i understand the example, if you declare an object of a super class but assign one of its subclasses to it (like MySuperclass class = new SubclasOfMySuperClass(); ) and you say something like class.someMethodOfMySuperClass() then that is in fact downcasting?

nicky's avatar

whoops in the example i gave it should have been class.someMethodOfMySUBCLASS() not superclass hehe

noyesa's avatar

I live in C++ land and we call it dynamic binding. In Java, all objects use dynamic binding so it’s kind of inconsequential but it makes a big difference in C++.

Downcasting/dynamic binding are tools used for polymorphism. As with your example, you can create a reference to a subclass, while that reference’s static type is actually the base class. With static binding, a lookup for a member function (method) would look only in the class that the reference is staticly bound too.

Consider a base class called “Vehicle”. It seems reasonable that we might make several more classes, like “Boat” or “Car” or “Rocket” and they would all have some sort of accelerate command. However, the actual process of acceleration is much different between each vehicle type, so we want only to call the acceleration behavior defined in that object’s particular subclass.

Suppose we have an array of Vehicles, where the static type of the array is Vehicle. However, we know that the list could contain objects of any subclass of Vehicle—Boat, Plane, or even types that haven’t been defined yet but could be added in the future, like Rocket.

If you wanted to iterate through this array and call the accelerate() member function on each one, you would want it to call the implementation of that function from the actual subclass of that object, not the base class Vehicle. In order to do this, function lookup is done at run time, and it looks for the definition in the dynamic type of the object, that being the actual subclass.

In C++, by default the static type is used, in which case the static type is an array of pointers to Vehicle type objects, so the compiler would by default look for the implementation of that function in Vehicle, unless it was declared virtual, which tells the compiler to delay lookup until run time. I know you don’t need to know that, but it makes the distinction a little clearer in the context of the language that makes you hold it’s hand when you want dynamic binding.

Downcasting in Java is when you cast a base class into a subclass and lookup is done at runtime. Let’s say you have a reference to an object of Dervived type whose static type is Base. You can convert that type to a Derived, a more specific class, using downcasting, using a type cast.

Base a = new Derived();
Derived b = (Derived)a;

nicky's avatar

ok, i get how to do the down casting now. But i know that in java if i were to call (from your example) an overriden method in the derived class i could just say

a.someOverridenMethodInDerivedClass();

and at runtime java would know to use the one from the derived class. Perhaps im just not getting the circumstances where one needs to downcast (or maybe what i said was off or incorrect)

Answer this question

Login

or

Join

to answer.

Mobile | Desktop


Send Feedback   

`