• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt related to casting  RSS feed

 
Dash Abhisek
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




I am not able to understand that bObject1 is of type boo and it contains a new Foo() which is upcasted to boo and when we call b0bject1.abc()--it is printing the line depending upon what object the reference variable bobject1
has in it...,compiler doesn't take into account the casting....but if i send
a new Integer(5) to a method expecting an Object as argument...,I have to downcast and get the Integer object...to be able to call Integer specific methods and if I don't downcast I can only access Object specific methods,so, my question is in this example
when Iam upcasting it (please refer line of confusion above) and then call bObject1.abc () why not it is printing "in boo" (that is boo specific method)


Thanks

[edit]Add code tags. CR[/edit]
[ August 02, 2008: Message edited by: Campbell Ritchie ]
 
Jigar Gandhi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friend
i understood your doubt ;
Since the methods are overridden the compiler chooses the method specfic to the object being created and not the reference type
when we upcast the reference it is still referring the Foo object and not a boo object. i hope you have understood else please refer to kathy sierra's book for scjp preparation
 
Dash Abhisek
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply...,it helped
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!