• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object casting

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class B extends A {
A a1 = new A();
B b1 = new B();
b1 = (B)a1;
}
Will the above code always compile but sometimes not run?
Also, can someone explain what 'casting down the tree means'. Is it the above?
[ May 22, 2002: Message edited by: Rob Petterson ]
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Petterson:
class B extends A {
A a1 = new B();
b b1 = new B();
b1 = (B)a1;
}
Will the above code always compile but sometimes not run?
Also, can someone explain what 'casting down the tree means'. Is it the above?


Let me try to answer it.
The code above will not compile as there is not a class named b.
If that is typo error, you can compile and run it well.
To know "casting down the tree", you should know a little about Java hierarchy is a inverted tree structure, i.e. parent class stays upside, and its child or grand-child or more will stay downside the tree.
So when saying "casting down", it means that you cast from a parent class to its child, it is not safe anyway.
By the way, upcasting goes the opposite way, so it is safe.
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Iuco, I made a couple of mistakes in the originals post's code. Can you have a look at it again? Thanks.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will compile
But will NOT nrun well
It will throw a ClassCastException for sure
because a1 can be a B, but in runtime, the JVM realizes that it is not. So the cast is ok because A and B are in the same line of the tree, but in runtime, they must be of the same class, or the casted one must be down the line of the tree.

The tree in this case would be
Object
|
|
A
|
|
B
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Paulo
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ultimately Rob,
You should remember the following rules about Casting:
1. Understand the "is a" relationship.
In this case, B "is a" A because it extends A. But A "is not a" B, rather it is the parent of B. Therefore, you can always cast an object of type B to its Parent type A.
Example

You can also do the following:

2. The following will cause a ClassCastException at Runtime but will compile just fine.

This is because the compiler does not really know what objects the variables a and b will represent at Runtime. For all it knows, these variables could be miraculously change by someother thread in such a way that the above cast will be legal at Runtime.
However, you the developer needs to know if the above will ever happen because the way you have coded it will surely cause a ClassCastException.
This is because B "is a" type of A but A "is not a" type of B.
3. Moreover the following code will also cause a ClassCastException and the class ClassTester will not compile.

[ May 22, 2002: Message edited by: Rodney Woodruff ]
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rodney for the explanation.
I'd imagine that b = (B)C; is illegal both at compile and run time due to B and C having no relationship between each other.
Q/
Is it necessary to have the cast, or can you get away without it?:
a = (A)b;
a = (A)c;
(I would try it myself, but the PC I'm on at work hasn't got a compiler on it).
[ May 22, 2002: Message edited by: Rob Petterson ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Classes B & C are both descendents of class A. Therefore, when you assign b or c to a, you're performing an "upcast." Upcasts don't require an explicit cast, so the following is perfectly legal:

Downcasts, on the other hand require an explicit cast:

I hope that helps,
Corey
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Corey, so just to really drive it home, what your'e saying is that downcasting ALWAYS needs a cast. Therefore it will be legal at compile time....but NOT at runtime??
Is that right?
 
luco zhao
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Petterson:
Therefore it will be legal at compile time....but NOT at runtime??
Is that right?


NOT ALWAYS, as there is a runtime check.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of it with the "is a" relationship.
Assume C extends B, which extends A.
An object of type C "is a" B. An object of type B "is a" A. Through transitivity, an object of type C "is a" A. In a more concrete example, image you have classes Animal and Cat. A Car "is a" Animal. Now, let's look at that in code:

All of these are examples of "upcasting," or casting to a higher level. In such a case, a cast in never required as the conversion can be done implicitly. Of course, the cast is allowed, it just isn't required.
Now, let's turn this around and look at casting down the tree or "downcasting." We said earlier that an object of type C "is a" A. But, what about the other way around. Can we say, an object of type A "is a" C? NO! That's not true. That's not more true than saying an Animal "is a" Cat. It might be a cat but, more than likely, it's something else, like a cow or a dog or something else.
Since the animal "might be" a cat, we need to tell the compiler explicitly that it is a cat, or it will yell at us. Let's look at the code:

In this case, the compiler will complain. The object referenced by animal is not, necessarily, a cat. In order to get around this, you must provide a cast:

In such a case, you're basically telling the compiler to trust you in that the object referenced my animal really is a cat. In this siutation, this is true. However, if the variable animal referenced an Animal object, like in the following example, your code would compile, but you'd get a run-time exception:

I hope that helps clear this issue up for you,
Corey
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks forthe detailed explanation Corey. Cystal clear now. It's one of those concepts that I think a lot of people can come un-stuck on.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. Just remember this rule:
You can use a child anywhere a parent is expected.
If you remember this (and understand it), you'll be fine.
Corey
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great explanation there, Corey.
Clement
 
Hey! You're stepping on my hand! Help me tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic