Can't convert a generics list of objects into a generics list ofinterfaces

Discussion in 'Java' started by Juergen Berchtel, May 20, 2005.

  1. In the following lines of code I get this compiler error (getUnits):
    Type mismatch: cannot convert from ArrayList<TestGen.Unit> to
    List<TestGen.UnitI>


    public class TestGen {
    public UnitI getUnit() {
    return new Unit();
    }

    public List<UnitI> getUnits() {
    return new ArrayList<Unit>();
    }

    public interface UnitI { }
    public class Unit implements UnitI {
    }
    }


    The conversation works well with one object in getUnit() but I can't
    convert the generics list.

    Thanks for any help
    Juergen
    Juergen Berchtel, May 20, 2005
    #1
    1. Advertising

  2. Re: Can't convert a generics list of objects into a generics listof interfaces

    Juergen Berchtel wrote:

    > In the following lines of code I get this compiler error (getUnits):
    > Type mismatch: cannot convert from ArrayList<TestGen.Unit> to
    > List<TestGen.UnitI>
    >
    >
    > public class TestGen {
    > public UnitI getUnit() {
    > return new Unit();
    > }
    >
    > public List<UnitI> getUnits() {
    > return new ArrayList<Unit>();
    > }
    >
    > public interface UnitI { }
    > public class Unit implements UnitI {
    > }
    > }
    >
    >
    > The conversation works well with one object in getUnit() but I can't
    > convert the generics list.


    You probably want

    public List<UnitI> getUnits() {
    return new ArrayList<UnitI>();
    }

    ArrayList<Unit> is not a subtype of List<UnitI> because if you have some
    other implementation of UnitI then it is incorrect to put an instance in
    an ArrayList<Unit>. Alternatively, you could declare the method to
    return a List<? extends UnitI>, but then you could never add anything to
    the returned List (without a type safety warning) because the specific
    Unit implementation used by the returned List would be unknown.
    Alternatively, you could return List<Unit>; this narrows the allowed
    types of the List elements, which may be what you need.

    If you're not sure which of those is most appropriate, then you need to
    go back and think some more about how the returned List is intended to
    be used. One of the nice things about Generics is that they do
    encourage you to think these things out.

    --
    John Bollinger
    John C. Bollinger, May 20, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Klaus Neuner
    Replies:
    7
    Views:
    486
    Klaus Neuner
    Jul 26, 2004
  2. 7stud
    Replies:
    11
    Views:
    688
    Dennis Lee Bieber
    Mar 20, 2007
  3. Royan
    Replies:
    8
    Views:
    753
    Patricia Shanahan
    Feb 15, 2008
  4. Soul
    Replies:
    0
    Views:
    517
  5. noydb
    Replies:
    12
    Views:
    339
    noydb
    Aug 19, 2011
Loading...

Share This Page