• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphisn - Should be easy right  RSS feed

 
Andrew Scott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello from sunny Wales,
I thought the polymorphic capabilities of Java would allow this. Am I doing something wrong.

For the example below assume that value and value2 are strings representing some value be it a decimal (Double) or a String. That we have already worked out what data type resides in the strings value and value as stored in the variable type.

With this code my compiler is telling me that the operator >= cannot be used on Object x. I thought that this object would now be handled as a Double due to polymorphism.

This is possible right?? or am I missing something really obvious?



Code:
=============================

Object x;
Object y;

if ( type.equals("double"))
{
x = new Double (value);
y = new Double (value2);
}
else (type.equals("String"))
{
x = new String(value);
y = new String(value2);
}

boolean b = x >= y;

================================



Thanks

Andrew.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Polymorphism would not apply here.

You can't use the comparison operators on objects whose reference type is Object.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to see this being simultaneously answered in two different forums.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:
Nice to see this being simultaneously answered in two different forums.

I couldn't find the other one. Maybe it's already been removed...?
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's on a different website. His last response was "Thanks so much thats great." so I'm assuming he got a satisfactory answer.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just something to keep in mind: People are less inclined to spend time helping when they find that the same question has already been answered in another forum. We can't control what happens elsewhere on the web, but when we find cross-posts in multiple JavaRanch forums, we close/delete them.

Having said that, are we really talking about polymorphism here? Or are we instead talking about why Double objects don't automatically unbox here?
[ August 23, 2006: Message edited by: marc weber ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrew Scott:
With this code my compiler is telling me that the operator >= cannot be used on Object x. I thought that this object would now be handled as a Double due to polymorphism.


Operators aren't polymorphic, only method calls are.

When the compiler looks at the line

boolean b = x >= y;

it doesn't know that x and y are Doubles. In fact, according to your code, they could well be Strings, but even that the compiler doesn't know. All it knows is that they are Objects.
 
Andrew Scott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for helping,

Yes I posted in more two forums of differing sites. I did not realise how superbly resposive you all would be.

Thanks ever so much .

The answer was to use the Comparable interface and not the comparison operators.


Job done thanks.

Andrew.

[ August 23, 2006: Message edited by: Andrew Scott ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!