Using Java generics in interfaces that return collections. Best practice? Pitfalls?

Instead. While it's basically the same, it avoids ugly warnings public interface IListable { //returns first n items from list public ArrayList getFirstNThings(int n); //returns last n items from list public ArrayList getLastNThings(int n); } Generally, it's not a problem to use a more specific return type in an implementation than in a super-type or interface. If you're dealing with IListable, you need to handle any object type in the returned list.

If you're dealing with GroceryList, you expect only GroceryItems. That's not only true for genric type arguments of return types, but for the return type itself as well.So if an interface specifies List get() is okay to implement it as ArrayList get().

Instead. While it's basically the same, it avoids ugly warnings. Public interface IListable { //returns first n items from list public ArrayList getFirstNThings(int n); //returns last n items from list public ArrayList getLastNThings(int n); } Generally, it's not a problem to use a more specific return type in an implementation than in a super-type or interface.

If you're dealing with IListable, you need to handle any object type in the returned list. If you're dealing with GroceryList, you expect only GroceryItems. That's not only true for genric type arguments of return types, but for the return type itself as well.

So if an interface specifies List get(), is okay to implement it as ArrayList get().

This method also allows you to make the GroceryList into a totally generic typed list for reuse later. – Thirler Feb 10 '10 at 18:09.

If both methods of IListable always return the same type, use this instead.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions