Can you program Java collections to an interface and use Serializable?
collections, interface, java, javabeans, serialization
Solution
If your class implements `Serializable` and all of its members are serializable, then the object can be serialized correctly. Example:
public class Person implements Serializable {
private String name;
private Collection<Integer> luckyNumbers = new ArrayList<Integer>();
}
As long as `luckyNumbers`'s instance is serializable (such as `ArrayList`), and its members are serializable (in this case `Integer`s) , then the object will serialize.
Problem
I've gone through and programmed all my domain level logic to interfaces. Now I find that when I want to put data in a bean, the bean doesn't work because the Collection interfaces (Collection, List, Set, etc) do not implement Serializable. Do I need to refactor all my code to use concrete types or is there a better course of action here?