• Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassCast Exception  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When will a ClassCast Exception be thrown?I am not able to understand the concept of upcasting and downcasting propely.Ranchers please help...
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
upcasting is done implicitly. So lets say a function calls for InputStream and FileInputStream is a subclass of InputStream. If you use FileInputStream it will auto cast up to InputStream.

downcasting is done explicitly. So you have to tell the compiler that you mean do that.
Example:
long a = 254;
int b = (int)a;

you are telling the compiler that you know you are downcasting and take responsibility that if variable a holds a value larger than what variable b can take that it will be truncated to fit variable b.

What you are saying is that you are trying to cast an object (someObject) to another object (anotherObject) where someObject is not a subclass of anotherObject. So you can not cast.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Classcast expection thrown when jvm cant cast one type to another
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't confuse casting primitives (eg int) with class casting. Primitive casting can always be done (except to or from boolean) and never throws an Exception and can return an apparently incorrect result. There are details in the Java Language Specification (JLS), but it is not an easy read. Note carefully what is called a widening conversion and what narrowing and whether you have to cast or not.

What you are calling an "upcast" is (I think) described in the JLS as a widening reference conversion; meaning altering the reference to an object to a more generalised and less specific type. If you have an inheritance hierarchy like the little one I drew a few weeks ago here, remember that java.lang.Object lives at the top of that tree.
If you try to cast a Car, Bus or Truck to a Vehicle, or to an Object that will always work; a Car is a Vehicle and it is an Object too.

But you might not be able to do it the other way, which I think is called narrowing reference conversion; if you try to cast a Vehicle to a Bus, it might actually be a Car . . .

Exception at line 12345: ClassCastException etc etc
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hope this link helps you http://forums.sun.com/thread.jspa?threadID=591501&messageID=3084545
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Upcasating means we are changing an object upto one level. For example when we assign a String object to an Object refernce, it is upcasting. It will do implicitly.

String s="Hi";
Object ob=s;

In the opposite way, ie, changing one level down, is the downcasting. It should be specified explicitly.Example,

Object ob=new String("Hi");
String s=(String)ob;

when the JVM cant cast the object, it will throw a ClassCastException

Object ob=new Car();
String s=(String)ob;
//This will throw an exception
 
Thomas Bennett
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think we are trying to be too technical being that this is the beginner forum.

either way if with an object or a primitive you have to tell the JVM if you are going down and not so if going up. If you want to call it narrowing or widening then feel free but that is also how it was explained when I took the SCJP so that is how I will re tell it to a person in the beginner forum.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Bennett:
I do not think we are trying to be too technical being that this is the beginner forum.
No, but we still like to use the correct nomenclature. As you say, correctly, a widening conversion, which is often called an "upcast," can be done implicitly, although there are rare occasions where a cast is needed.

A narrowing conversion, often called a "downcast," usually requires an explicit cast; in the case of a narrowing primitive conversion it might lose information, but a narrowing reference conversion doesn't lose information whether it is to the correct type or the wrong type. It simply throws an Exception! I think you are right on both counts.

I am sorry if I wasn't clear earlier; I suddenly realised I had to go somewhere else and I was late.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!