The Java Program: List.java

  1 // List.java -- illustrate the use of an interface with lists
  2 
  3 interface Printable {
  4    void print ();
  5 }
  6 
  7 // Two unrelated classes each with a "print" method
  8 class Cake implements Printable {
  9    public void print () { System.out.println ("Cake"); }
 10 }
 11 
 12 class Furniture implements Printable {
 13    public void print () { System.out.println ("Furniture"); }
 14 }
 15 
 16 
 17 public class List {
 18 
 19    public static void main (String args[])  {
 20       Cake german = new Cake();
 21       Furniture chair = new Furniture ();
 22       Cake upside = new Cake();
 23       Printable[] list = {german, chair, upside};
 24 
 25       for (int i=0; i<list.length; i++) {
 26          list[i].print();
 27       }
 28    }
 29 }