• 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 imposssible!!

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic