2

According to my use-case, I have a class myClass implementing an interface myInterface.

public class MyClass implementing MyInterface{
  ...
}

I have got a method which has List as parameter.

myMethod(List<MyClass> listOfMyClass){
 ...
// want to make a call to third party API which has List<MyInterface>
thirdPartyAPI(listOfMyClass);
}

thirdPartyAPI(List<MyInterface> listOfMyInterface){
...
}

In the above case, I want to use listOfMyClass in calling #thirdPartyAPI. Currently, I got "The method myMethod(List) is not applicable for arguments thirdPartyAPI(List).Let me know if I need to update my design or approach. Any help will be appreciated. Thanks.

Been through Java Generics -- Assigning a list of subclass to a list of superclass but didn't help my case.

Siddharth Shankar
  • 489
  • 1
  • 10
  • 21
  • 2
    You can always use a `List` on your site and pass this list to the 3rd party API. If this is not an option and you cannot change the 3rd party API (which really should be changed because of PECS), then I am afraid you have to create a new `List` and copy all entries from `listOfMyClass` to the new list. – Turing85 Jun 18 '19 at 21:17

1 Answers1

2

perhaps create a new list of the necessary type, if you're unable to change the method signature:

thirdPartyAPI(new ArrayList<MyInterface>(listOfMyClass));

If the method were defined as taking a Collection<? extends MyInterface> you could then pass in your list as is.

jspcal
  • 50,847
  • 7
  • 72
  • 76