Search code examples
javainterfacemethod-signature

Error while calling interface parameter method with implemented class


I have a method that receives 2 parameters: List of interface. I get an error when I try to call this method with List of class that implements that interface.

The input must be of type class, I don't want to change it to List of interface.

List<Io> oldList;
List<Io> newList;
handleIo(oldList, newList); //Here I get the error

public void handleIo(List<IIo> oldIos, List<IIo> newIos) {...}

public class Io implements IIo {...}

public interface IIo {...}

I thought this is the idea of interfaces, I can't figure out what is wrong. The error asking to change the type of the parameters I send to the method.


Solution

  • First of all you shouldn't start your method with a capital letter (naming conventions are important).

    But to your problem: Change the method signature to the following:

    public void handleIo(List<? extends IIo> oldIos, List<? extends IIo> newIos)