Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Need a downcast example

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book only has the "Dog D = (Dog) Animal;" example for downcasting and shows that it compiles but then throws an exception at runtime.

So what's an example of a downcast where it works, and why would you need it?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example of downcasting.

One place you need it is if you override the equals method inherited from Object. Because the parameter to the method is of type Object, you have to cast it to the type of the class to be able to access methods and variables defined in it.


[ May 15, 2006: Message edited by: Keith Lynn ]
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shawn,

Here's an example of a valid downcast.

class Dog {}
class Dogma extends Dog {}
class Dogmatic extends Dog {}

Dog dogwood = new Dogma(); // a Dogma object is upcast to a Dog
Dogma bush = (Dogma) dogwood; // a Dogma object is downcast from a Dog

You cannot upcast a Dogmatic to a Dog and then downcast it to a Dogma. You also cannot downcast a Dog to a Dogma. In the working example, above, the dogwood references a Dogma, so you can downcast the Dogma.

Hope this helps.
Steve
 
Shawn Kuenzler
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great stuff, guys. Thanks!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether a downcast succeeds at runtime depends on the true runtime type of the object -- that is, what was used with "new."

For example, suppose you have an Animal reference "a." You can satisfy the compiler by explicitly downcasting to type Dog...

Dog d = (Dog)a;

But whether this works at runtime depends on the true runtime type of "a." If you had...

Animal a = new Animal();

...then the true runtime type of "a" is Animal, and a downcast to type Dog will fail because an Animal (the object created) is not really a Dog. On the other hand, if you had...

Animal a = new Dog();

...then a downcast of "a" to type Dog will succeed, because the object referenced by "a" is, in fact, a Dog.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic