Marvin
Marvin

Reputation: 1706

Polymorphism mixing Objects, Lists and Interfaces

my today's problem is about those 3 functions :

public void generateCalendarEvents(final String id,final Map<String,String> params);    
public void generateCalendarEvents(Object object,final String id,final Map<String,String> params);
public void generateCalendarEvents(List<Identifiable> objets,final Map<String,String> params);// Identifiable is an Interface

They are called according to the context, such as the followings:

generateCalendarEvents(someString,params); // params is a Map<String,String>
generateCalendarEvents(this,someString,params);
generateCalendarEvents(this.getDataList(),params); // getDataList return a list of objects implementing the "Identifiable" interface

The 2 firsts work well, but I get a compilation error from the third one saying :

The method generateCalendarEvents(String, Map) in the type AbstractController is not applicable for the arguments (List, null)

I don't understand there why the compiler cannot match this call with the third function... and why it's confusing with the first when at least one could think that it could confuse between List and Object. It may be basic, but I don't see the solution here.

Any idea here?

Thanks in advance.

PS : obviously here, I could simply give a different name to all three methods. I just would like to dig a little deeper in polymorphism.

Upvotes: 2

Views: 74

Answers (1)

Marvin
Marvin

Reputation: 1706

Such a shame..... the problem was not about polymorphism. But often, a question clearly stated helps finding the solution.

So the problem was about the way I sent the List< Idenfiable>: I assumed the getDataList() method returning a List would be cast as a List< Identifiable>..

And it is not...so the way to solve the problem was :

List<Identifiable> rdvs = new ArrayList<Identifiable>();
rdvs.addAll(this.getDataList());              
generateCalendarEvents(rdvs,new HashMap<String,String>());

Any comment here is welcome... cause even though I managed to have it work, I am not absolutely sure about why I cannot do it the previous way.

Upvotes: 1

Related Questions