• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting imposssible!!  RSS feed

 
hanihanan younis
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Developer...
i like the java for it's object oriented feauture..
im shocked that i cant make casting for this issue:
Object object;
Apple apple;
Orange orange;

if(condition)
{
object=apple;
}
else
{
object=orange;
}
my Wish that i can use the object like what the condition applied..
in other words:
i want the object to be outer this scope either apple or orange
Not object
please Help with any idea
with my respect to all who love to help or even try..
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16046
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not really clear what you want exactly. Where do you want to cast what? Java has all the object oriented features you need, so I suspect that you have a misunderstanding somewhere about how OO works, not about something that's missing in Java.

You can assign apple and orange to object without casting because class Object is a base class of class Apple as well as of class Orange (in fact, class Object is the base class of all classes in Java).

If you want to find out later what kind of object the variable 'object' really points to later on in the program, you can use the instanceof operator, for example:

If you cast an object to something that it isn't, you will get a ClassCastException at runtime. For example:

[ February 02, 2006: Message edited by: Jesper de Jong ]
 
Jean Mathew
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just check it once more... there is nothing wrong in assigning apple to object...

r u getting any compilation error?
if you have tried with this code in a method (with nothing else other than the 'condition' variable is a boolean) then you have to set apple and oragne to null or should be initialized. ok
 
hanihanan younis
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first thanks alot ...
second:
what the idea i try to do it..
i have a flag if it true i want to use e.x range all the web application.
else i want to use the apple instead..
the apple and orange in my web application are 2 MODELS..so
i think if i made a thired object from kind of Object..then decide which model to process & get data either for apple or orange..using the object that at runtime it will be decided to be orange or apple without make a lot of changing in each CONTROL.. & a lot of conditions inside..
Thanks again alot to all of you...
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect what he wants is the compile-time type of the variable to change based on the runtime conditional evaluation. Stated this way, I hope hanihanan understand why this can't be.

But in any event, what you need to learn about is polymorphism. Once you understand that, you'll understand why you didn't need to worry about this question. Read this article for a gentle introduction to the topic.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!