The Artima Developer Community
Sponsored Link

Java Answers Forum
ClassCast Exception

2 replies on 1 page. Most recent reply: Dec 10, 2002 10:20 PM by Prabhu

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Savvy

Posts: 1
Nickname: savvy
Registered: Dec, 2002

ClassCast Exception Posted: Dec 9, 2002 8:59 PM
Reply to this message Reply
Advertisement
Please help me regarding when do we get Classcast Exception.
I am really confused.I tried
Object obj=new Object();
String str=(String)obj;
This code is giving me claas cast exception.

On the other hand I have written application in which i am doing:
Derived class=(Derived)Parent;
It is running perfectly alright.Kindly help me whether there are compatibility issues involved.Thanx.


hardeep

Posts: 1
Nickname: harry
Registered: Dec, 2002

Re: ClassCast Exception Posted: Dec 10, 2002 10:06 PM
Reply to this message Reply
Dear friend
You are trying to downcast from a higher type to lower since Object class sits top in the hierarchy.This cast throws the exception.String is an Object type but Object is not necessarily String
cheers!

Prabhu

Posts: 2
Nickname: feefee
Registered: Dec, 2002

Re: ClassCast Exception Posted: Dec 10, 2002 10:20 PM
Reply to this message Reply
hay instead,if u really wanna have Object as string..then u can use 'String str = obj.toString();

Flat View: This topic has 2 replies on 1 page
Topic: Applet Previous Topic   Next Topic Topic: Simple questions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use