• Post Reply Bookmark Topic Watch Topic
  • New Topic

A question on downcasting  RSS feed

 
Tushar Ravi
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • 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.

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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?
 
Will Myers
Ranch Hand
Posts: 419
Java jQuery Scala
  • Mark post as helpful
  • send pies
  • 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;
 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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 Java Oracle
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Ravi wrote:
Abimaran,

Will check the forum before I post ;)

You are Welcome!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!