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(); } }
Cloneable
interface
and the
clone()
method?
Serializable
interface
and the
readObject()
method?
// Concrete implementation built on abstract skeleton static ListIn Item 20, Bloch advises programmers to prefer interfaces to abstract classes. Explain Bloch's list of advantages: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) { // ??? } }; }
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
.