| 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.
|
 |
 |
|
|
subject: ClassCastException
|
|
|