aspose file tools
The moose likes Beginning Java and the fly likes ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "ClassCastException" Watch "ClassCastException" New topic
Author

ClassCastException

saurav sarkar
Ranch Hand

Joined: Jan 07, 2007
Posts: 180

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


Be Objectively Oriented.Explore the power of OOPs.
My Blog, Eclipse EMF Query committer.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

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.


[Jess in Action][AskingGoodQuestions]
Muthukumar Chellappa
Greenhorn

Joined: Aug 02, 2007
Posts: 9
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

Joined: Aug 02, 2007
Posts: 9
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

Joined: Apr 20, 2006
Posts: 3327

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Mehrez MAROUANI
Greenhorn

Joined: Aug 16, 2007
Posts: 2
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


--<br />SCJP 5.0 (in progress)
Justin Samuel
Greenhorn

Joined: Aug 16, 2007
Posts: 8
Excellent way of explaining by Ernest Friedman-Hill and Mehrez MAROUANI.

These 2 replies helped a lot in clearing concepts.


Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

A similar discussion with a different explanation and example is here.

Hope this may add some values!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException
 
Similar Threads
assignment
Thread Quest from Marcus Green Exam1
StringBuffer
StringBuffer == and equals
StringBuffer question?