SWE/CS 332 In Class Exercise # 19

Name(s):

public class Super {
   public Super() {
      overrideMe();
   }
   public void overrideMe () {
   }
}
public final class Sub extends Super {

   private final Date date;  // filled in by constructor

   public Sub() {
      date = new Date();
   }
   @Override public void overrideMe () {
       System.out.println(date);
   }

   public static void main (String[] args) {
      Sub sub = new Sub();
      sub.overrideMe();
   }
}
  1. What is the pattern, and how common is it?

  2. What does the main method do, and why?

  3. Which of Bloch's rules does this example break?

  4. What does this example mean for Cloneable interface and the clone() method?

  5. What does this example mean for Serializable interface and the readObject() method?

  6. To what extent does this rule generalize to producer methods? Hint: Think about the relationship between subtyping and immutability.

// Concrete implementation built on abstract skeleton
static List intArrayAsList(final int[] a) {
   if (a == null) throw new NPE(…);
   // Note anonymous class
   return new AbstractList() {
     public Object get(int i) {
        // ???
      }
      public int size() { 
        // ???
      }

      public Object set(int i, Object o) {
        // ???
      }
   };
}
In Item 20, Bloch advises programmers to prefer interfaces to abstract classes. Explain Bloch's list of advantages: and disadvantages

Bloch notes that the biggest downside of an interface can be mitigated with a carefully designed companion class. To explore how this works, fill in the missing code above (5 lines total!) to create a complete implementation of a (fixed size) List that is backed by an int array. The companion class in this case is AbstractList.