Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting

 
Shane Timlin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working through casting and came across the following in K&B Chapter 2.




The CastTest2 class runs successfully. The crux of this is Dog d = (Dog) animal; A Dog reference is referring to an Animal object which has been cast to a Dog. This seems to make sense. I'm telling the JVM to treat <d> the Animal as if it is a Dog.

The bit that is confusing me is a little later in the chapter. The same Dog d = (Dog) animal; down cast is used. It compiles ok but the JVM doesn't like it.
I've created a similar class to run it to highlight the error I get.



When I execute this, I get -
generic noise
bark
bark
bark
Exception in thread "main" java.lang.ClassCastException: Animal cannot be cast to Dog
at DogTest.main(DogTest.java:18)
So why does the down cast work in an if statement but not as part of a class?

Thanks in advance

Shane
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shane,

First of all, a warm welcome to the CodeRanch!

So why does the down cast work in an if statement but not as part of a class?

The instanceof operator will check if the object animal is of type Dog. If it is, you can safely perform the cast. If it isn't, no cast is performed (you don't want to cast a Cat instance to a Dog reference variable).

So you are actually experiencing the same behavior in classes CastTest2 and DogTest. But there is one huge (and very important) difference: using the instanceof operator you can avoid wrong casts. If an invalid cast is performed, you'll get the runtime exception ClassCastException (as you discovered in class DogTest.
In the class CastTest2 you have 3 elements in the Animal array: 2 Animal objects & 1 Dog object. So if you would run this class without the if statement, you'll get a ClassCastException as well (because the 1st element is an Animal which is not a Dog). The instanceof operator guarantees (when used appropriately of course) you can safely cast without having to fear a ClassCastException. That's why you'll see 3 outputs (3 Animal objects in the array) of the makeNoise method (2x generic noise and 1x bark) and just 1 output of the playDead method (just 1 of these 3 Animal objects is a Dog and passes the instanceof check).

Hope it helps!
Kind regards,
Roel

PS. When posting code, please UseCodeTags. It improves the readibility of your post. Because you are new here, I added them for you
 
Shane Timlin
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for explaining Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to hear I could help!

Another nice to know: instead of making a "thank you" post, you could also the post(s) which you liked. It's easier, faster and other ranchers will see immediately which are the "starred" posts.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic