Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassCastException  RSS feed

 
Kamila Bertran
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hey there! I hope you're having a good weekend, but still find a minute (or two) to help me out with this one.

So I am doing this test and have this piece of code:



According to me (and JVM) the output of this program is:

Twinkling Star
Twinkling Star
Twinkling Star


But, according to whoever it was that prepared the test the program should throw a ClassCastException at runtime.
Is there something I am missing? Could someone, please, explain to me what's happening in this code so if I fail similar question on the test I can make my case?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37230
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamila,
Where did the test come from? Maybe it has a typo?
 
Campbell Ritchie
Marshal
Posts: 55682
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a good point of Jeanne's; we shou‍ld know the source, to confirm there is no copyright problem, and also so we can assess the quality of the sources. Maybe one o fus will have a copy of the book themselves.

So, I tried your code and the strangely‑named class Bob has a main method which runs without Exceptions and prints
Twinkling Star
Twinkling Star
Twinkling Star
The class Sun extends Star so it is a Star, and it implements Universe so it is a Universe. The ob2 has a runtime type of Sun, so when ob3 is assigned to ob2, ob3 also has a runtime type of Sun, so you can cast it to Sun (redundant) Star and Universe. If you really are supposed to have a class cast Exception, either you have not quite copied what was in the book, or there is a mistake in the book. Have you been through the errata for the book? If it is a mistake, there is a 90+% chance that somebody else has found it and reported it.

I fancied a bit of playing around with reflection, so I enhanced your Bob class:-Run that code and see what happens. It shou‍ld confirm that you can cast obj3 to any of those types. The cl local variable is probably redundant. You can even enhance that with this sort of method, which shou‍ld return true for all attempts.
 
Blake Edward
Ranch Hand
Posts: 106
4
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the original code, line 13, shouldn't the method be doStuff and not soStuff?
 
Campbell Ritchie
Marshal
Posts: 55682
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blake Edward wrote:In the original code, line 13, shouldn't the method be doStuff and not soStuff?
Probably. But when I copied and pasted the code it compiled and ran without error or exception, so I don't think the additional method explains the original question.
It does make it possible there there is another minor copying error which would explain the problem, however.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!