aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting" Watch "Casting" New topic
Author

Casting

Roger Chan
Ranch Hand

Joined: Nov 13, 2001
Posts: 30

I wonder why the last statment will cause run-time error, ClassCastException. With the explicit casting before object reference a, downcasting should be allowed.
Thanks
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
You can only cast 'a' to a Truck if it is a Truck or subclass of Truck. 'a' is an Automobile. If 'a' was created like,
<code>
Automobile a = new Truck();
</code>
Then it would not cause an exception.
[This message has been edited by Jim Hall (edited November 30, 2001).]
vishal avad
Ranch Hand

Joined: Nov 29, 2001
Posts: 45
hi roger
class casting is just a facility by which u can tell compiler that u want to convert this object to another.
so it compiles fine. but the conversion is done at runtime with respect of the object which actually resides inside the variable.
as they r different so it cause run-time error
Roger Chan
Ranch Hand

Joined: Nov 13, 2001
Posts: 30
Which statements, when inserted at line 1, will cause a runtime exception ?

(a) b=b1;
(b) b2=b;
(c) b1=(B1)b;
(d) b2=(B2)b1;
(e) b1=(B)b1;
Explanation for (d): It will not compile because b1 can never refer to an object of class B2
Is this because what b1 and b2 refer to have no inheritance relationship?
Explanation for (e): The following will compile:
b1=(B1)(B)b1;
The above statment upcasts and then downcasts back to the original reference type. Is it doing anything at all?
Thanks
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
Sometimes it helps to use examples that are more concrete, making it a bit easier to understand.
<code><pre>
class Pet {}
class Dog extends Pet {}
class Cat extends Pet {}
public class ExtendsTest{
public static void main(String args[])
{
Pet generic = new Pet();
Dog fido = new Dog();
Cat felix = new Cat();
// insert statement here
}
}
(a) generic=fido;
(b) felix=generic;
(c) fido=(Dog)generic;
(d) felix=(Cat)fido;
(e) fido=(Pet)fido;
</pre>
</code>
Your answer for (d) is correct. They have no inheritance relationship. They are siblings of the super class and siblings cannot be assigned to each other, even with a cast. As above, it doesn't make much sense to cast a dog object into a cat object.
I don't see the benefit of upcasting an object, then downcasting to the original object. But it is legal. This is not the case with primitives. If you cast a double to an int and then back to a double, information is lost.
[This message has been edited by Jim Hall (edited November 30, 2001).]
Roger Chan
Ranch Hand

Joined: Nov 13, 2001
Posts: 30

<code><pre>
class Pet {}
class Dog extends Pet {}
class Cat extends Pet {}
public class ExtendsTest{
public static void main(String args[])
{
Pet generic = new Pet();
Dog fido = new Dog();
Cat felix = new Cat();
// insert statement here
}
}
(a) generic=fido;
(b) felix=generic;
(c) fido=(Dog)generic;
(d) felix=(Cat)fido;
(e) fido=(Pet)fido;
</pre>
</code>
QUOTE]
Hi Jim,
Thanks for your explanation. Concrete example does make things a lot easier to understand.
Roger
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
to Roger:
This is why the JVM launches a runtime exception for t=(Truck)a;
If this were allowed an object of class Automobile will be pointed by a variable of type Truck. Imagine there would be a method inflate8Wheels in class Truck but not in Automobile. The compiler would allow t.inflate8Wheels. But this would imply that a method is invoked on a type of object for which it was not thought. inflate8Wheels should only be invoked on Truck objects, because it was not defined in Automobile objects.
Briefly. This prevents treating an object of type as it were an object of a different type.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting