1

I'm working on a web development project, and I'm working on several apps with "news feed" functionality. I want to make sure that every item with a picture flips a switch back and forth, so that pictures alternate on the left and right, even if some items in between didn't have pictures. So, I created an interface:

public interface TextWithImageTile {

    public abstract String getOrientation( ) ;

    public abstract void setOrientation( String orientation ) ;

}

Now, all of my "news feeds" use news items implementing that interface, regardless of whatever custom fields each one uses. So, I had expected to be able to instantiate this utility class to process lists of items implementing that interface:

public class AlignmentAssigner {

    List<TextWithImageTile> listToAssign ;

    public AlignmentAssigner(List<TextWithImageTile> list) {

        this.listToAssign = list ;

    }

    public void alignList( ) {

        for ( TextWithImageTile listItem : listToAssign ) {
            ...
        }

    }

}

But now, when I try to send it a List of HomeItem classes - which implement the TextWithImageTile interface - it complains of a type mismatch. Here's the implementation:

List<HomeItem> homeItems = homeDAO.getHomeItems();

AlignmentAssigner alignmentAssigner = new AlignmentAssigner( homeItems );

I'm told that, "The constructor AlignmentAssigner(List) is undefined." Do I have something crossways here? Is it possible to have a method process all implementations of a particular interface, so long as it is only asked to operate on fields and methods defined by said interface? If so, how - or what needs to be changed in my code?enter image description here

rainydaymatt
  • 594
  • 3
  • 10
  • 21

1 Answers1

1

You should use

public AlignmentAssigner(List<? extends TextWithImageTile> list) {
    ...
}
aioobe
  • 413,195
  • 112
  • 811
  • 826