Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting Questions

 
Rob Beast
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Beast
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic