• Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassCastException  RSS feed

 
saurav sarkar
Ranch Hand
Posts: 180
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a very basic question and may be i have
missed very basic concept.

I have this code



the statement when casting o1 into sb1 is giving me a runtime exception of ClassCastException.

My confusion is if sb1 is not a sub class of Object it will give me
compile time error.But when in this case StringBuffer is a sub class
of Object i am getting the runtime exception.
I should be able to cast the super class to sub class ideally.

The ClassCastException API says

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.


I do not understand what is meant by 'instance'.

Please help me.

Thanks in advance,
Saurav
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A cast of a reference type is not a magic spell that turns one kind of object into another. A cast changes exactly nothing. All that a cast does is tell the compiler "You don't know it, but this variable really points to an object of this other type." So, for example, this is legal and runs correctly:

Object o1 = new StringBuffer();
StringBuffer sb = (StringBuffer) o1;

It's legal code because StringBuffer is a subclass of Object. But it actually runs because o1 really does point to a StringBuffer object. Your code compiles for the same reason, but fails because your Object variable is really pointing an an Object, not a StringBuffer.

The word "instance" means "example of" or "one of". An instance of a class is an object belonging to that class. The class of an object never changes, although variables of various types may refer to it.
 
Muthukumar Chellappa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuffer sb1=new StringBuffer("new one");
sb1=(StringBuffer)o1;
Object o=sb;
System.out.println("the output:"+o.toString());
System.out.println("the new otuput"+o1.toString());
[/CODE]

the statement when casting o1 into sb1 is giving me a runtime exception of ClassCastException.

My confusion is if sb1 is not a sub class of Object it will give me
compile time error.But when in this case StringBuffer is a sub class
of Object i am getting the runtime exception.
I should be able to cast the super class to sub class ideally.

The ClassCastException API says

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.


I do not understand what is meant by 'instance'.

Please help me.

Thanks in advance,
Saurav[/QB]
 
Muthukumar Chellappa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Remember one point, In compile time the compiler sees only the type of the referrence, not denoting the current object. But in runtime it sees denoting the current object, not the type of the referrence.

If we try to typecast the reference, we change the type of the referrence, not denoting the current object, it only cheat the compiler, it will be caughted in runtime(that is exception).

Regards,
C.Muthukumar
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
A cast of a reference type is not a magic spell that turns one kind of object into another. A cast changes exactly nothing. All that a cast does is tell the compiler "You don't know it, but this variable really points to an object of this other type." So, for example, this is legal and runs correctly:

Object o1 = new StringBuffer();
StringBuffer sb = (StringBuffer) o1;

It's legal code because StringBuffer is a subclass of Object. But it actually runs because o1 really does point to a StringBuffer object. Your code compiles for the same reason, but fails because your Object variable is really pointing an an Object, not a StringBuffer.

The word "instance" means "example of" or "one of". An instance of a class is an object belonging to that class. The class of an object never changes, although variables of various types may refer to it.



Excellent example EFH.
 
Mehrez MAROUANI
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The casting is changing a variable reference from one type to another in condition the two type are in the same inheritance tree. There are two types of casting : downcasting and upcasting.

--Downcasting is casting from a parent to a child class.
Example :

Object o = new StringBuffer("foo");
StringBuffer sb = (StringBuffer) o;
In this example, we're casting the object o of type Object to a StringBuffer object. This is legal because o at runtime refers to a StringBuffer object although it has a reference variable of type Object.

--Upcasting is casting from a child to parent class.
Example:


StringBuffer sb = new StringBuffer();
Object o = (Object) sb;

Upcasting is implicit so you could write in the last example:
Object o = sb;
This is legal because sb is an Object !

Hope this helps

Bye
 
Justin Samuel
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent way of explaining by Ernest Friedman-Hill and Mehrez MAROUANI.

These 2 replies helped a lot in clearing concepts.


 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A similar discussion with a different explanation and example is here.

Hope this may add some values!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!