• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting

 
Roger Chan
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<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
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic