1

I have an issue with the method idTag of Q class. I'm not sure what type should be put instead of XXXX. I've tried < T extends Taggable< T >> but there is an issue with the following call : Q.idTag (b) ;

It says :

The method idTag(List) in the type Q is not applicable for the arguments (ArrayList< RedElem>)

< T extends Taggable< T >> Doesn't seem to be the correct answer but i'm running out of idea, I don't know what could be the right one thank you

interface Taggable<T> {

  void tag(T t);

  T getTag();
}

class Elem implements Taggable<Elem> {}

class Obj implements Taggable<Obj> {}

class RedElem extends Elem {}

class Q {

  static <XXXX> void idTag(List<T> l) {
    for (T e : l) e.tag(e);
  }

  public static void main(String args[]) {
    ArrayList<Elem> a = new ArrayList<Elem>();
    ArrayList<RedElem> b = new ArrayList<RedElem>();
    ArrayList<Obj> c = new ArrayList<Obj>();

    idTag(a);
    idTag(b);
    idTag(c);
  }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Jennie87
  • 11
  • 1

1 Answers1

0

What you are trying to say in your static method is that the list should contain Taggable items, that's not what you're declaring. You need

static <T, E extends Taggable<T>> void idTag(List<E> tList, T tagWith) {
    tList.forEach(e.tag(tagWith));
}

or if you really want to tag each item with itself (which doesn't make a lot of sense):

static <E extends Taggable<E>> void idTag(List<E> tList) {
    tList.forEach(e-> e.tag(e));
}
daniu
  • 14,137
  • 4
  • 32
  • 53