Why do I get compiler errors with this Java code?
1 public List extends Foo> getFoos()
2 {
3 List extends Foo> foos = new ArrayList
Use this instead:
1 public List extends Foo> getFoos()
2 {
3 List foos = new ArrayList(); /* Or List */
4 foos.add(new SubFoo());
5 return foos;
6 }
Once you declare foos as List extends Foo>, the compiler doesn't know that it's safe to add a SubFoo. What if an ArrayList had been assigned to foos? That would be a valid assignment, but adding a SubFoo would pollute the collection.