• 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

A question on downcasting

 
Greenhorn
Posts: 10
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

This is my first post in the forum, and definetly not the last

I have a doubt on the downcasting concept. I am trying to convert Object to String.

Object obj = new Object();

String s = obj; // This throws compilcation error.


To prevent the compilation error I change the code to
String s = (String) obj ; // Downcasting ...

But then i get a runtime error for class cast exception.

In that case how can i ever cast a parent to child, and does the downcast concept really work?

Any help appreciated .

Sorry for posting it here. First time user

Thanks

Tushar.

 
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

Tushar Ravi wrote:
In that case how can i ever cast a parent to child, and does the downcast concept really work?



Choose a correct forum first Please. And, How do you achieve this in the real world scenario?
 
Ranch Hand
Posts: 462
Scala jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this won't work because obj is not a String, what you need to do is:

Object obj = new String();

String s = (String) obj;
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are few things you need to consider when you are casting(better to avoid such situations ;) )-
You need to specify the cast explicitly in cases of Downcasting.
Also casting would check if the instance is actually of that type or can be cast to that type before carrying out the cast. So always use- instanceof check before doing the cast.

Also you have a Beginning Java forum where you can post this. Also there are lot of queries asked related to this before.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand on what will said...

String extends Object. So all Strings are Objects, but not all Objects are Strings.

Because all Strings are Objects, upcasting is "safe," and no explicit cast is needed. For example...

...is implicitly upcasting a String reference to an Object, and assigning that reference to "obj." You could include an explicit (Object) cast, but it's not needed.

But downcasting is different, because not all Objects are Strings. Therefore, an explicit cast is needed. When you say...

...you (the programmer) are telling the compiler, "Yeah, we both know that obj is type Object, but I happen to know it's really pointing to a String. Trust me on this."

Because of your explicit cast, the compiler will trust you, and you will avoid the compilation error. However, if what you said is turns out to be false -- if the obj is not really pointing to a String -- then an exception will be thrown at runtime.

Basically, an explict downcast will only work if the reference was previously upcast. That is, it's pointing to an object that's really a subtype of the reference.
 
Tushar Ravi
Greenhorn
Posts: 10
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to all of you for your valuable comments. Marc you explanation was awesome .

Abimaran,

Will check the forum before I post ;)
 
Abimaran Kugathasan
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

Tushar Ravi wrote:
Abimaran,

Will check the forum before I post ;)


You are Welcome!
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic