• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

ArrayList - how to store objects of certain kind

Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm new and I am trying to store an ArrayList of Term objects. The Term objects represent terms in a polynomial. (A polynomial is a math expression like 4x^3 + 3x^2 - 2x + 4.) I have 4 files. One is a Term class, one is a Polynomial class, one is a PolynomialInterface and the last is a tester file program.

My Terms are simply objects with 2 instance variables: coefficient and exponent. In it, I have 2 constructors (one that takes no parameters and one which does). I have the getters and setters to extract and change the coefficient and exponent variables. Finally, I have a toString method.

In the PolynomialInterface (which I was given and must use to solve this problem), I have a myriad of unimplemented methods.

In the Polynomial class, I created it to inherit from ArrayList and implement 2 interfaces: PolynomialInterface and Cloneable

My first constructor takes no arguments and looks like this:

Later on down the code, I began to try and implement an add() method which is supposed to take in a Polynomial object and add it to the current Polynomial object. However, there's this error in my code:
Type mismatch: cannot convert from Object to Term

Here's how I began to implement the add() method.

So, then I thought "Ok, this.get is returning an Object and not a Term. I don't want to bandaid it with a type-cast. How do I construct the Polynomial ArrayList so that it only holds Term objects?"

And that's my question in a nutshell. If I left anything out, let me know!

And THANK YOU for being here to support us newbies!

Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Patricia, Welcome to JavaRanch

An ArrayList (or any collections class for that matter) can only hold an item of type Object. This is so polymorphism can work its magic, since all classes untimately inherit from type Object, an ArrayList can ultimately hold any type of object. If the ArrayList class took specific types of objects, they would have to have an ArrayList class to hold every type of Class in the JDK. And then that wouldn't even be enough, because then how would developers store their custom objects?

Therefore, in Java v1.4.2 and below, you always need to cast the object you get out of a collections class back to the type of Object it is. This is not considered a band-aid; it is considered proper coding and using polymorphism to its fullest. (If you look through the source code of the JDK itself, you will see lots of usage of casting.) In Java 5.0 (a.k.a. v1.5), when creating a collection, such as an ArrayList, you can predefine what type of Object the ArrayList will hold. This is called Generics, and as mentioned, it was added in v5.0. Then when you pull the Object out, it comes out as the type of Object you defined that collections class to hold. Ultimately, however, the ArrayList is still holding items of type Object and a cast is still being done. When you compile the code in Java 5, it "inserts" all the appropriate casting for you in the final bytecode. The main advantage is that you eliminate the Chance of a ClassCastException since the validity of the casting is checked at compile time rather then runtime.

I won't get into the details of Generics here, since I do not know what version of Java you are using (or are required to use), plus it's a big topic. You can Google it to find some articles or tutorials on it, and then ask some additional question on it if that is the route you want to go.

I hope that helps.

[ February 19, 2005: Message edited by: Mark Vedder ]
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark: Excellent response!
    Bookmark Topic Watch Topic
  • New Topic