Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding a method

 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I sort of got into this in another post, but it's kind of buried. I hope it's not a double/cross post.

I'm extending a class, and need to change the units accepted by the method "addPoint(int x, int y)" to accept doubles instead.

From what I've read, I should just be able to do something like this in my class:


This works as far as compiling and letting my JSP run, but my data comes back as "0" for every value of x or y I send as a double.



My JSP below shows how I load my data (with both ints and Doubles) and the tests I've used to check them


This code returns the following on my page


Thanks guys,
Mike
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried overriding based on the primitive double ?

addPoint( 0.1, 5.0 ) would be matched with addPoint( int, int ).
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pho Tek:
Have you tried overriding based on the primitive double ?

addPoint( 0.1, 5.0 ) would be matched with addPoint( int, int ).


Thanks, I think that's what I did when I extended the class here:


Is there a difference? I tried modifying mine to use lower case "double x, double y", but I get an error (java.lang.NoSuchMethodError).

Thanks!
Mike
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There definitely is. Double is the wrapper object for the primitive double.

In this case, you are not actually overriding. The term is overloading.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this code works.
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pho Tek:
this code works.

Thanks for the reply Pho. I still don't seem to be able to get it working. In my case, the class Polygon is an imported class (awt package), and while you've defined it as "public void addPoint( int ix, int iy )" (ix and iy to specify that it takes integers), I can't actually go into the class and edit it, and I'm not sure what it actually looks like.

I currently have the following in my class, but the method does not seem to be getting overloaded. I'm sorry if I'm doing something dumb - I'm still so new to all this...


You can see I don't have the references to ix and iy, because I don't think those values exist in the polygon class. Can you clarify? Maybe I need to override them too?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike --

You know you want to add an addPoint() method, and you've got that part figured out. But I don't think you've thought much about what you want that method to do. You want a point added this way to actually appear as part of the Polygon, yes? Which means that you have to somehow get those doubles shoved into the Polygon's data structures. To do so, you're (sadly) going to have to convert them to ints, since the Polygon class works with int arrays. You could do something like this:

public void addPoint(double x, double y) { addPoint( (int) x, (int) y ); }

Now, of course, converting to ints is going to truncate the fractional parts of your doubles. This is a limitation of the Polygon class. If you need floating-point points, then Polygon isn't going to work for you. You could look at java.awt.geom.GeneralPath and see if that meets your needs.
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
... you're (sadly) going to have to convert them to ints, since the Polygon class works with int arrays. You could do something like this:

public void addPoint(double x, double y) { addPoint( (int) x, (int) y ); }

Now, of course, converting to ints is going to truncate the fractional parts of your doubles...


Ernest - Thanks for giving it to me straight. Not as easy as I'd hoped for my first real world java app... The polygon class does a lot of things I would like to take advantage of, so I'm thinking of multiplying all of my data by 100,000,000 prior to sending the polygon class so that values like 43.6678652 get turned into ints like 4366786520. Then I can do everything I need, and when I pull them back out, I'll just divide by the same. Does this sound reasonable?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, people do indeed do that sort of thing sometimes. Be careful, because that number you give is only about a factor of 20 smaller than the largest positive int value; otherwise you'll get overflow.

Also, I forgot to mention before: remember, overriding refers to redefining a method with another method with an identical signature (name and argument list) in a subclass. Overloading is what you're doing: adding a new method by the same name, with a different argument list, in the same class or in a subclass.
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, 4366786520 is already outwith the limits of the int primitive type.
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Yes, people do indeed do that sort of thing sometimes. Be careful, because that number you give is only about a factor of 20 smaller than the largest positive int value; otherwise you'll get overflow.


Thanks for the warning - I checked my data, and I know the maximum amount, and it still falls within the int range (thank goodness!).

It seems to make sense that I should impose my multiplication at the addPoint event, and I'm not sure how to do that. When Pho was explaining he described the following for overriding an existing classes method:

What I don't understand here is that it seems to suggest going into that overridden class (polygon, above, with ix and iy) and changing things. I must be misinterpreting.

I think what I need to do is something like

Obviously this is wrong (since I just tried it...). I think I'm still at ground zero regarding overriding methods. Are there any FAQs?

Thanks!
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how you would do it:
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pho Tek:
This is how you would do it:


Aaaaaaahhhhhh!!! Thanks - that's super!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic