Bounded Wildcards : Bounded Types « Generics « Java Tutorial






class Two {
  int x, y;

  Two(int a, int b) {
    x = a;
    y = b;
  }
}

class Three extends Two {
  int z;

  Three(int a, int b, int c) {
    super(a, b);
    z = c;
  }
}

class Four extends Three {
  int t;

  Four(int a, int b, int c, int d) {
    super(a, b, c);
    t = d;
  }
}

class Gen<T extends Two> {
  T[] coords;

  Gen(T[] o) {
    coords = o;
  }
}

// Demonstrate a bounded wildcard.
public class MainClass {
  static void showTwo(Gen<?> c) {
    System.out.println("X Y Coordinates:");
    for (int i = 0; i < c.coords.length; i++)
      System.out.println(c.coords[i].x + " " + c.coords[i].y);
    System.out.println();
  }

  static void showThree(Gen<? extends Three> c) {
    System.out.println("X Y Z Coordinates:");
    for (int i = 0; i < c.coords.length; i++)
      System.out.println(c.coords[i].x + " " + c.coords[i].y + " " + c.coords[i].z);
    System.out.println();
  }

  static void showAll(Gen<? extends Four> c) {
    System.out.println("X Y Z T Coordinates:");
    for (int i = 0; i < c.coords.length; i++)
      System.out.println(c.coords[i].x + " " + c.coords[i].y + " " + c.coords[i].z + " "
          + c.coords[i].t);
    System.out.println();
  }

  public static void main(String args[]) {
    Two td[] = { new Two(0, 0), new Two(7, 9), new Two(18, 4), new Two(-1, -23) };

    Gen<Two> tdlocs = new Gen<Two>(td);

    System.out.println("Contents of tdlocs.");
    showTwo(tdlocs); // OK, is a TwoD

    Four fd[] = { new Four(1, 2, 3, 4), new Four(6, 8, 14, 8), new Four(22, 9, 4, 9),
        new Four(3, -2, -23, 17) };

    Gen<Four> fdlocs = new Gen<Four>(fd);

    System.out.println("Contents of fdlocs.");
    // These are all OK.
    showTwo(fdlocs);
    showThree(fdlocs);
    showAll(fdlocs);
  }
}
Contents of tdlocs.
X Y Coordinates:
0 0
7 9
18 4
-1 -23

Contents of fdlocs.
X Y Coordinates:
1 2
6 8
22 9
3 -2

X Y Z Coordinates:
1 2 3
6 8 14
22 9 4
3 -2 -23

X Y Z T Coordinates:
1 2 3 4
6 8 14 8
22 9 4 9
3 -2 -23 17








12.5.Bounded Types
12.5.1.Bounded Types
12.5.2.Using Wildcard Arguments
12.5.3.Bounded Wildcards
12.5.4.Upper/lower bound for a wildcard