The Java Program: Example.java

  1 /*
  2   A few simple interfaces illustrating some potential capabilities
  3   to be used in the following class definitions.
  4 */
  5 
  6 interface Drawable {
  7    public void draw ();
  8 }
  9 
 10 interface Printable {
 11    public void print ();
 12 }
 13 
 14 interface Storable {
 15    public void store ();
 16 }
 17 
 18 interface Dealable {
 19    public void deal ();
 20 }
 21 
 22 /*
 23   We imagine that it is important that "Bicycle" extends "Vehicle".  But
 24   a "Bicycle" has other capabilities as documented in the "implements"
 25   clause and certified by the compiler.
 26 */
 27 
 28 class Vehicle {
 29    public void go () {
 30       //  Do something
 31    }
 32 }
 33 
 34 class Bicycle extends Vehicle implements Printable, Storable {
 35    public void store () {
 36       //  Do something
 37    }
 38 
 39    public void print () {
 40       System.out.println ("Bicycle");
 41    }
 42 }
 43 
 44 
 45 /*
 46   Again we imagine that it is important for "CardPlayer" to extend "Player".
 47   Also a "CardPlayer" has other capabilities as documented in the "implements"
 48   clause and certified by the compiler.
 49 */
 50 
 51 class Player {
 52    public void play() {
 53       // Do something
 54    }
 55 }
 56 
 57 class CardPlayer extends Player implements Dealable, Printable, Drawable {
 58    public void draw () {
 59       //  Do something
 60    }
 61 
 62    public void print () {
 63       System.out.println ("CardPlayer");
 64    }
 65 
 66    public void deal () {
 67       //  Do something
 68    }
 69 }
 70 
 71 /*
 72   Abstract classes, interfaces, and super classes can all be used to group
 73   objects into collections that have like interfaces.
 74 */
 75 
 76 public class Example {
 77    public static void main (String[] args) {
 78 
 79       // Things that go ...
 80       Vehicle[]   v = {new Bicycle(), new Vehicle(), new Vehicle()};
 81 
 82       // Things that play ...
 83       Player[]     g = {new Player(), new CardPlayer(), new Player(), new Player()};
 84   
 85       // Things that draw ...
 86       Drawable[]  d = {new CardPlayer(), new CardPlayer(), new CardPlayer()};
 87 
 88       // Things that print ...
 89       Printable[] p = {new Bicycle(), new CardPlayer(), new Bicycle(), new Bicycle()};
 90 
 91       // Things that store ...
 92       Storable[]  s = {new Bicycle(), new Bicycle()};
 93 
 94       // Things that deal ...
 95       Dealable[]  r = {new CardPlayer(), new CardPlayer(), new CardPlayer()};
 96    }
 97 }