Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

General API class question

 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I've found a class in the API that does almost everything I need it to do. However, in addition to what it does, I also need it to store a couple extra variables. The class is java.awt.polygon, and I need it to store simple variables like a name, id and description - no additional methods. It seems like this should be do-able. Is this a case where I am extending an existing class? Can someone point me to a place where I can learn more about how to do this?

Thanks!

Mike
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike McMahon:
...s this a case where I am extending an existing class? ...

That's exactly right. You want something that IS-A Polygon, but has extended capabilities. You do this with the keyword "extends"...
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marc,

This is looking like a textbook example of a simple extension of a class.

In the example you've shown, I could then do stuff like :

Given that addPoints(int x, int y) is a method of the original polygon?

Thanks!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly.

Note: Watch the capitalization. "myPolygon" is not the same as "MyPolygon."
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, don't have any pointers to examples.

You still might want to consider a containment relationship rather than an inheritance one. Depending on how much you want to use the new class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic