2

Possible Duplicate:
Why we can't do List<Parent> mylist = ArrayList<child>();

I've a question on polymorphism and invoking methods.

My service layer signature is public void saveRules(String paramOne, String paramTwo, List<RuleDTO> rules)

My EvaluationRuleDTO extends from RuleDTO

So from my controller I attempt to perform the following:

service.saveRules(String paramOne, String paramTwo, List<EvaluationRuleDTO> rules).

But this is not allowed as it complains about List<EvaluationRuleDTO> rules not being List<RuleDTO> rules.

This does not make much sense to me. Is this a weakness in the Java language, or what concept am I missing here?

Thanks

Community
  • 1
  • 1
DJ180
  • 18,724
  • 21
  • 66
  • 117

1 Answers1

4

It is happening because the polymorphism is applied in the List type, not in its generics.

Edit 1: Michael posted a comment with a usefull link.

Edit 2: You can do (from Why we can't do List<Parent> mylist = ArrayList<child>();):

List<? extends RuleDTO> list = yourEvaluationRuleDTOList;
Community
  • 1
  • 1
davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
  • OK, I get it now, makes sense. So what would be a good solution here to my problem? I think something using generics but I'm not 100% sure – DJ180 Jul 23 '12 at 22:47
  • @DJoyce: I complemented my post using the tip presented in the link. – davidbuzatto Jul 23 '12 at 22:50
  • Now I get this error: The method saveRules(String, String, List) is not applicable for the arguments (String, String, List) – DJ180 Jul 23 '12 at 22:59
  • Define your method like this and then use your original `List` parameter: `public void saveRules(String paramOne, String paramTwo, List rules)` – Anthony Accioly Jul 23 '12 at 23:04