The Java Program: OrderedList.java

  1 interface Orderable {
  2    boolean lessThanOrEqual (Orderable other);
  3 }
  4 
  5 /*
  6    A subtying constraint on a parameteric type:
  7    "bounded quantification" or "bounded genericity"
  8 */
  9 class OrderedList <T extends Orderable> {
 10 
 11    public void insert (T elem) {
 12       elem.lessThanOrEqual (elem);
 13    }
 14 
 15 }
 16 
 17 class OrderedList2 <T extends Orderable> {
 18 
 19    public void insert (Orderable elem) {
 20       elem.lessThanOrEqual (elem);
 21    }
 22 
 23    public T poly (T elem) {
 24       return elem;
 25    }
 26 
 27    public T poly2 (T elem) {
 28       elem.lessThanOrEqual (elem);     // OK
 29       return elem;                     // OK
 30    }
 31 
 32    /*
 33    public T poly2 (Orderable elem) {
 34       elem.lessThanOrEqual (elem);     // OK
 35       return elem;                     // Mustn't return type Orderable
 36    }
 37    */
 38 
 39 }
 40 
 41 /*
 42    A subtying constraint on a parametric type where the
 43    constraint may depend of the type parameter:
 44    "F-bounded quantification" or, "recursively bounded quantification" or,
 45    "recursively bounded genericity"
 46 */
 47 
 48 class OrderedList3 <T extends Comparable<T>> {
 49 
 50    public void insert (T elem) {
 51       elem.compareTo (elem);
 52    }
 53 
 54 }