• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Casting question  RSS feed

 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this has been asked recently feel free to direct me to the right post...

I understand it is possible to downcast a parent object to a child object. For example:

Asuusming child extends parent
Parent p = new Parent();
Child c = new Child();

c = (Child)p;

When would this be necessary? And when would this be a bad idea? Thanks.
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted an example of this yesterday under this topic. See if that makes sense, and come back with any questions.
 
Sheriff
Posts: 21459
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Tony Smith:
When would this be necessary? And when would this be a bad idea? Thanks.


you should do this if:
1) you are 100% sure the object is a Child, or a subclass of Child (p instanceof Child returns true). Your object inheritance tree might ensure that.
2) you need the extra methods / fields that Child provides and Parent does not.

If 1) does not hold you may get a ClassCastException if the is not a Child. If 2) does not hold then there is no reason to cast.
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following on from Rob's post, this kind of thing would work:

The compiler assures us the parameter is a Parent or some extension of it, but it can't guarantee that the parameter is a Child. If we left out the instanceof test and somebody passed in a Parent, the (Child)x cast would throw an exception.

This kind of test and cast is often a sign of a design that we should improve, but sometimes it's hard to avoid.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Tony Smith:

Parent p = new Parent();
Child c = new Child();

c = (Child)p;



Note that your code, as written, is not valid. We know that the run-time type of p is Parent, not Child (because we can see that you instantiated it that way). Casting is appropriate where the run-time type is actually a child, and it is not, here.

Also, note that you are creating a new Child object and assigning it to c, only to be overwritten in the next line. Are you sure you understand the difference between defining a variable, and initializing it?

- Adam
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!