java method not applicable to the arguments (interfaces) -


i new java , haven't used interfaces much, , imagine basic question.

i define interface, second interface extends it, , class implements second interface:

public interface i1 {   public void dostuff(); }  public interface i2 extends i1 {   public void domorestuff(); }  public class c2 implements i2 {   public void dostuff(){     // implements i1 required   }   public void domorestuff(){     // implements i1 required   } } 

i want invoke method has argument collection of objects implement interface i1:

public class c3 {    public static void doi1stuff(list<i1> implementorsofi1){       // code here    } } 

but abovementioned error when try like

c2 ac2 = new c2(); list c2l = new list<i2>(); c2l.add(ac2); c3.doi1stuff(c2l); 

what i've done seems reasonable me java novice. why doesn't work, , more experienced programmer do? hope i've made simple mistake somewhere , possible...

(revised question: works (as replies stated) if dealing single c2 object, not collection of them...)

update

ooops, c2l list, not list`. should compile -- , does others commented -- though dangerous same reason describe below. leave answer here now, can delete if find not useful


 public static void doi1stuff(list<i1> implementorsofi1)  ...  c3.doi1stuff(c2l); 

doi1stuff expects list<i1> passing list<i2> not allowed. strange sounds, "a bag of apples not a bag of fruit". reason being can put any fruit in bag of fruit, e.g. banana can't bag of apple.

to relate example consider there class c1 extends i1. doi1stuff can add c3 paramer, breaking contract. might clarify realize parameter named wrong, should listofimplementorsoft1.

one way fix problem change declaration

 public static void doi1stuff(list<? extends i1> implementorsofi1) 

this says parameter list of some unknown type extends i1. list<i2>, list<c2>, list<c1> meet satisfy constraint. declaration, method cannot add list, because doesn't know type is.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -