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

ClassCastException confusion

 
Mark Justison
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

First time poster/future certification taker with a question from the OCA Java SE 7 guide by Mala Gupta.

Regarding class cast exceptions, there's this bit of code that I cribbed from the book:



This throws a ClassCastException. Okay, I can follow along. I believe this occurs because inks, which is an ArrayList of Ink, can store objects of BlackInk but it can’t know at compile time what it is going to index.
Next comes this:



This cannot compile because the ArrayList inks cannot store an object of BlackInk, the object at the index doesn’t become a factor (as far as I can tell).
However, this little snippet comes a bit later:



This compiles and fails at runtime with the ClassCastException. To me, the line ‘printable = (Printable)blackInk’ in the class ClassCastExcp2 would run into the same issue as the line ‘Ink ink = (BlackInk)inks.get(0)’ in the class InvalidListAccess. They are both trying to cast to something they can’t cast to, so why is one a compilation error and the other a runtime error?ClassCastExcp2

Hopefully I explained this clearly enough. Thanks in advance!
-Mark
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,

First of all, a warm welcome to CodeRanch!

Mark Justison wrote:This throws a ClassCastException. Okay, I can follow along. I believe this occurs because inks, which is an ArrayList of Ink, can store objects of BlackInk but it can’t know at compile time what it is going to index.

You are correct! inks is an ArrayList of Ink, so all objects which pass the "IS-A Ink" test can be stored in this list, in this example Ink, BlackInk and ColorInk instance (but maybe in the future also WaterproofInk and EdibleInk. So when you get an item from this list, you know 1 thing for sure: the object will be an Ink. But it could be an Ink, BlackInk or ColorInk object. If you cast the object to the wrong type, you'll get a ClassCastException (as shown in your code snippet). You could use the instanceof operator to check if the object is of a given type before you cast to that type (preventing this runtime exception to be thrown). Code might look like:

Mark Justison wrote:This cannot compile because the ArrayList inks cannot store an object of BlackInk, the object at the index doesn’t become a factor (as far as I can tell).

The reason why you get a compiler error here, is because BlackInk and ColorInk are classes from different class hierarchies. If both classes were in the same class hierarchy (e.g. ColorInk extends BlackInk) you would not get a compiler error, but a runtime exception. More info about different class hierarchies and its consequences can be found in this thread. Note: it's about the instanceof operator, but the same rules apply to casting as well.

Mark Justison wrote:This compiles and fails at runtime with the ClassCastException. To me, the line ‘printable = (Printable)blackInk’ in the class ClassCastExcp2 would run into the same issue as the line ‘Ink ink = (BlackInk)inks.get(0)’ in the class InvalidListAccess. They are both trying to cast to something they can’t cast to, so why is one a compilation error and the other a runtime error?ClassCastExcp2

When interfaces are involved it gets tricky If the BlackInk class would be final, the ClassCastExcp2 code snippet would result in a compiler error instead of a runtime ClassCastException. That sounds a little bit weird at first, but if you know the reason why (which can be found in this thread) it all makes sense.

Hope it helps! If you still have doubts/questions after carefully reading the threads I mentioned, don't hesitate to post a reply in this thread.
Kind regards,
Roel
 
Mark Justison
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply! I figured it had something to do with interface weirdness. I'll have a look at those links.

- Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic