1

I am getting the error:

Error:(6356, 38) java: incompatible types: java.util.Map<java.lang.String,net.windward.format.wordml.WordMLParser.WordMLControl> cannot be converted to java.util.Map<java.lang.String,net.windward.format.OfficeMLParserBase.IOfficeXmlControl>

Where the class I am passing is:

private abstract static class WordMLControl implements IOfficeXmlControl {
    ...
}

And the method is:

protected void setProcessControl(Map<String, IOfficeXmlControl> procs) {
    ...
}

Why is this an error - I think this should be fine as the objects implement that interface.

Update: Ok, I understand this issue now (thank you Jorn). But the question remains, what's a good solution for this? In my case I will always get either Map<Cat> or Map<Dog> and that map is only read from.

user207421
  • 305,947
  • 44
  • 307
  • 483
David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • @JornVernee - it looks like even generics won't get me around this. Good explination on the link you provided, but no good solution to this problem that I saw. – David Thielen Jan 14 '18 at 21:20
  • 2
    If you strictly want to take values out of the map, you could use: `Map` as a parameter type (i.e. [PECS](https://stackoverflow.com/questions/2723397)). That will allow you to pass a `Map`. – Jorn Vernee Jan 14 '18 at 21:23

1 Answers1

0

If you declare a method

protected void setProcessControl(Map<String, IOfficeXmlControl> procs)

the passed parameter can only be a Map<String, IOfficeXmlControl>. If you want allow all implementing classes, you have to change the declaration to

protected void setProcessControl(Map<String, ? extends IOfficeXmlControl> procs)

If that's not an option, you can do a bad hack when you pass your map to the method:

setProcessControl((Map<String, IOfficeXmlControl>) (Map) myMap);

Instead of an error you get a compiler warning but the call should work.

Lothar
  • 5,323
  • 1
  • 11
  • 27