• 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

casting object references.

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, i have a doubt in object reference casting, in the following code:


from what i understand ##2 should also produce a runtime error, but obviously it isnt. please help me understand the correct logic

thanks and regards
 
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

d1 = (Dog)a; ##2 this compiles and runs though a is not an istanceOf dog, how is it still being downcast to a Dog



IT GIVES RUN TIME ERROR AS WELL ,,, PLEASE TRY AGAIN!
 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hama Kamal wrote:

d1 = (Dog)a; ##2 this compiles and runs though a is not an istanceOf dog, how is it still being downcast to a Dog



IT GIVES RUN TIME ERROR AS WELL ,,, PLEASE TRY AGAIN!



Apologies, yes it does. i was skipping a line in my original code, which makes all the difference. Sorry for wasting your time.
 
Hama Kamal
Ranch Hand
Posts: 144
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sorry for wasting your time



You are most welcome my friend, never mind..
 
Ranch Hand
Posts: 206
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommended do a search first for similar topic. See my post and discussion on a similar topic for more info.

https://coderanch.com/t/544105/java-programmer-SCJP/certification/downcasting-or-upcasting-ref-variable

You need an "instanceof" test before the downcast to ensure, otherwise, it will throws an exception.

//d1 = (Dog)new Animal(); ##1 this gives a runtime error as Animal is not an instance of Dog

It give a runtime error simply you were creating an anonymous object instead of Animal with statement "new Animal();" on the cast. Change it to "d1 = (Dog)a;" like you did and it will fix the problem.


Here is a recap and hope it help:

 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey thanks Tommy, i understand.
reply
    Bookmark Topic Watch Topic
  • New Topic