import java.util.*; public class PolymorphicCollectionExample{ //Here we choose what collection to make public static Collection makeCollection(boolean sorted){ if(sorted) return new TreeSet(); else return new HashSet(); } //Here we can use the Collection without knowing its //concrete type public static void main(String[] args){ Collection col = makeCollection(true); col.add(new Integer(1)); col.add(new Integer(2)); col.add(new Integer(3)); col.add(new Integer(4)); System.out.println(col); } }