This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "ClassCastException" Watch "ClassCastException" New topic
Author

ClassCastException

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3829


why this program is giving ClassCastException ...
thanks .
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
Look carefully at the ClassCastException. It tells you the line numbers in the file which are generating the exception. What is the first line in the stack trace? Which part of the program does it refer to?


"I'm not back." - Bill Harding, Twister
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
You receive a ClassCastException because you are trying to cast a Object to a "My" object reference.

Remember, you can do upcasting implicitly. That is to say, you can assign any object of a subclass to an object of its parent class. The contrary is not always true.

You can do downcasting explicitly by means of the cast operator, however the object has to be assignable to the reference variable receiving the casted reference.

By assignable I mean this casted object has to be of the same class or a subclass of the variable receiving the casted reference.

That is why your code failing at runtime:



Hope it helps.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: ClassCastException
 
Similar Threads
Khalid Mock - Hashcode Q
equals()
confusing Generics
equals and hashCode??
Fun with stacks