tags:

views:

134

answers:

3

Can anyone explain the problem with this? The 'foo' class fails to compile with an 'incompatible types' error. If you replace 'UniqueList' with 'ArrayList', it works correctly, even though UniqueList is an unmodified subclass of 'ArrayList'. 'Column' is a class defined in the app, and I don't see how its details would matter.

public class UniqueList<E> extends ArrayList<E> {}

public class foo {
    protected UniqueList<Column> a = new UniqueList<Column>();
    public void bar (){
        for (Column c : a) {
        }
    }   
}

I realize that according to Java goodness rules, the definition of 'a' should refer to the interface 'List', but that causes an 'unchecked conversion' warning on that line. Which I also don't really understand despite Googling about that error for a while (most posts assume you are pre-generics and are missing the qualifier).

Thanks as always.

+1  A: 

Compiles fine here. I didn't have your Java definition for Column, so I replaced it with "Integer" but otherwise unchanged.

> javac -version
javac 1.6.0_15
Steven Schlansker
+3  A: 

Compiles fine under JDK 1.6.0_06. I don't know why it shouldn't..

Jack
A: 

The following compiles fine, with no warnings on the declaration of a

import java.util.ArrayList;
import java.util.List;

public class foo {
    public static class UniqueList<E> extends ArrayList<E> {}
    protected List<String> a = new UniqueList<String>();
    public void bar (){
        for (String c : a) {
        }
    }
}

Java version is 1.6.0_17

Jim Garrison