java - Why would a method that accepts an interface reject an implementation of that interface? -


i trying figure out answer problem in textbook, having trouble. question asking me have input array checked see if contains object fits feature. every feature of seems working fine except when attempt implement method using input 1 of classes implementing interface.

example:

main(){   boolean r1 = has(input, checkfor2); }  public static <t> boolean has(t[] input, check<t> c){   // check algorithm , returns boolean }  static public interface check<t>{   boolean iscontained(t item); }  static public class checkfor2 implements check<integer>{   public boolean iscontained(integer val){     // check algorithm   } }  // other check algorithms implementing check<t> follow well. 

the error getting when "has" called in main method. says:

the method has(t[], main.check<t>) in type main not applicable arguments (int[], main.checkfor2)

its suggestion change class in method specific class instead of interface defeats purpose of writing way.

have made kind of rookie mistake in coding this?

the problem unrelated interface. has fact giving primitive int array int[] instead of integer[] array.

in method

public static <t> boolean has(t[] input, check<t> c){ 

the type t inferred give parameter. in case, giving int[] , check<integer>. however, an int[] cannot boxed integer[], type inference fails: t can't inferred integer.

the solution therefore change primitive array integer[] array, , send first parameter method.


Comments