aspose file tools*
The moose likes Beginning Java and the fly likes Casting Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting Questions" Watch "Casting Questions" New topic
Author

Casting Questions

Rob Beast
Greenhorn

Joined: Oct 24, 2004
Posts: 2
My questions are in the code.

Thanks for any help guys.

*1 - Why does this print out B? Is it because when b1 was upcasted to a2, the print method was overridden?

*2 - Why when printed, does b2 still have a bnum? How did a2 keep the bnum variable from b1?

*3 - Did it always have it but keep it hidden?

[ edited to remove long comments/questions from code -ds ]
[ October 25, 2004: Message edited by: Dirk Schreckmann ]
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Function calls are done "polymorphically" -- which is just a big word for "at runtime". So, if you call a method on an object at runtime, it will execute the function that that class declared (here's the kicker), not the function for the class of whatever you casted it to.

new Parent().invoke() will invoke Parent.invoke().
new Child().invoke() will invoke Child.invoke().

And, assuming that Child extends Parent:

((Parent) new Child()).invoke() will still invoke Child.invoke() despite it being cast to a Parent before the method does its magic.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Rob Beast
Greenhorn

Joined: Oct 24, 2004
Posts: 2
Thanks for the quick response Nathaniel, it makes sense now.

How about the bnum though? A2 just went crazy and kept it hidden. Is this normal behavior or is there something else I'm missing?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Rob Beast:
How about the bnum though? A2 just went crazy and kept it hidden. Is this normal behavior or is there something else I'm missing?
You're missing one fine point: casting doesn't modify an object instance. In contrast to method dispatching (the polymorphism that Nathaniel explained) which happens at runtime, casting happens to variables at compile time.Assuming Bar extends Foo, casting foo to a Bar tells the compiler to assume that the instance to which foo points will actually be a Bar (or one of its subclasses) at runtime. The JVM will perform a check at runtime and throw ClassCastException if it fails.

So in your code, assigning the B b1 to the variable a1 doesn't modify the instance to which b1 (and now a2) points. To keep it clear, remember that variable type declarations are only for the compiler. At runtime, an object's type is what matters.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Rob Beast:
How about the bnum though? A2 just went crazy and kept it hidden. Is this normal behavior or is there something else I'm missing?


a2 is not an object, but only a reference to an object. When you say

a2 = b1;

all you are saying is "let a2 point to the same object b1 is pointing to" - the object itself isn't touched at all, and therefore still is an instance of class B.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Casting Questions