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.
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:
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.
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.
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
posted
0
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).
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:
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.
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: