File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Quick ArrayList Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Quick ArrayList Question" Watch "Quick ArrayList Question" New topic

Quick ArrayList Question

Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
Hey guys, Let's say I have three classes: Polygon, Rectangle, and Oval. Both Oval and Rectangle extend Polygon. So now I'm creating a new ArrayList that I want to hold Polygons and anything that is-a polygon (Rectangle, Oval, etc.).

How can I make an ArrayList take Polygon objects, and any object that is a subclass of Polygon?

Just simply doing this:

Does not work.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32819

What error do you get? The following compiles and runs.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Ben Jass
Ranch Hand

Joined: Sep 25, 2010
Posts: 76
Pointless thread, I guess it works now heh.
I agree. Here's the link:
subject: Quick ArrayList Question
It's not a secret anymore!