CS/SWE 332 In Class Exercise Number 17
October 25, 2017


Consider a generified version of Liskov's SumSet example.

public class SumSet <E> {
   private List<E> els; 
   private E s; 
   private Adder<E> a; 

   public SumSet(Adder<E> p) {
      els = new ArrayList<E>(); a = p; s = p.zero();}

   // E: if x is null throw NPE; if x cannot be added to this
   // throw CCE; else adds x to this and adjusts the sum
   public void insert(E x) {
       E z = a.add(s, x);
       if (!els.contains(x)) {
          els.add(x); s = z;

   //E: return sum of elements in this
   public E sum() {
      return s; 
  1. We'll start with the related subtype approach.

    What does Adder<E> look like?

    What does Adder<Integer> look like?

    Work through a simple client interaction.

  2. Let's switch to the element subtype.

    What does Addable<E> look like?

    How does Integer have to change? How likely is that?

    How does SumSet have to change? Consider the constructor as well as the insert() method.

    Work through a simple client interaction.