In an effort to understand Generics in Java (and how to use them), I am trying to implement a list structure using them but have hit a wall.
Suppose there is some classes (we'll call them ClassA, ClassB, and ClassC) which all extend ClassBase. ClassBase is abstract, and simply contains an ID code with appropriate getters and setters. The subclasses add additional attributes, but those aren't important.
The list is designed to be able to hold a number of the above sub-classes, and manipulate the list using the common ID attribute. The list needs to be growable, and each list will only contain one type of sub-class (so a list of ClassA, ClassB, or ClassC. No mixed combinations of those).
I set up my List class like so:
ClassList<T extends ClassBase>
I then declare an array of elements as "private T list", and write the remainder of the class. The signatures for the element getters and setters are as follows, since those might be important:
public T get(int index)
public void set(int index, T item)
Everything appears to be good, except when I go to compile it complains about every instance where I try to recreate the underlying array (in order to shrink/grow it).
.\test\ListClass.java:10: error: generic array creation
list = new T;
So after some thought and reading the error makes sense, as the compiler doesn't know what constructors are going to be available (at least that's how I understand it), so it doesn't appear that this way will work.
I keep the same class declaration, but change the internal array to "private ClassBase list", and then adapt my code accordingly. The main change is casting from ClassBase to T and back when necessary. So the getter for instance looks like this:
public T get(int index)
So now my code compiles and executes, but every instance where I have done the above casting now throws the infamous unchecked warning:
.\test\ListClass.java:16: warning: [unchecked] unchecked cast
I can see why the warning is appearing, but at the same time the ListClass controls all means of accessing and manipulating the list, so I can guarantee that only elements of type T are placed in there.
I suppose I could suppress the warnings. But I am of the belief that they are there for a reason and would prefer to get rid of them the proper way. The problem is I can't work out what the 'proper' way is (or whether infact suppressing the error is the proper way in this case).
So that's my question. What is wrong with my design, and how do I fix it?
Thanks in advance.
Before anyone tells me to use one of the existing generics and stop reinventing the wheel, that's not the point. My aim isn't to recreate a replacement for the generic lists, it is to understand generics, and a list like above is about the best example I can think of which clearly shows how they work.