• 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

downcasting doubt

 
Ranch Hand
Posts: 237
MyEclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is no use of down casting right?

Dog dog = (Dog)animal;
it compiles but fails at execution time,so there is no use of downcasting am i right.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This wouldn't fail at runtime:)
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is an interesting observation. Its ok at compile time, but you will always get a class cast exception at runtime.
 
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will not fail at runtime.
Can you please provide the actual code?
 
KrishnaPrasad raghavan
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Harpreet Singh janda wrote : This will not fail at runtime. Can you please provide the actual code?



You are right. I meant the following won't run.

Given this code.



This will compile fine but will throw a class cast exception during runtime.

But the following will compile fine



superClass reference is pointing to the SubClass object. So the line



Compile and runs fine.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the actual object is SubClass (Super class reference variable holds Sub class instance). you can cast it.
 
Harpreet Singh janda
Ranch Hand
Posts: 317
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Super class can be used to hold a reference of subclass and the underlying object is always the subclass (if super class is having reference to subclass). A superclass can have multiple subclasses so at compile time it will be acceptable if you are having a superclass object that is pointing to first subclass and the you are casting that superclass object to 2nd subclass.
But this will fail at run time as when jvm will try to cast the underlying object to 2nd subclass, it will found that both the objects are different so it will through ClassCastException.
reply
    Bookmark Topic Watch Topic
  • New Topic