Enable interface for mgc version.
[IRC.git] / Robust / src / Tests / InterfaceTest.java
1 public interface Instrument {
2   // Compile-time constant:
3   int VALUE;// = 5; // static & final
4   // Cannot have method definitions:
5   void play(int n); // Automatically public
6   void adjust();
7 }
8
9 class Wind implements Instrument {
10   public Wind(){}
11   public void play(int n) {
12     System.out.println("Wind.play() " + n);
13   }
14   public String what() { return "Wind"; }
15   public void adjust() { System.out.println("Wind.adjust()"); }
16 }
17
18 class Percussion implements Instrument {
19   public Percussion(){}
20   public void play(int n) {
21     System.out.println("Percussion.play() " + n);
22   }
23   public String what() { return "Percussion"; }
24   public void adjust() { System.out.println("Percussion.adjust()"); }
25 }
26
27 class Stringed implements Instrument {
28   public Stringed(){}
29   public void play(int n) {
30     System.out.println("Stringed.play() " + n);
31   }
32   public String what() { return "Stringed"; }
33   public void adjust() { System.out.println("Stringed.adjust()"); }
34 }
35
36 class Brass extends Wind {
37   public Brass(){}
38   public String what() { return "Brass"; }
39 }
40
41 class Woodwind extends Wind {
42   public Woodwind(){}
43   public String what() { return "Woodwind"; }
44 }
45
46 public class InterfaceTest {
47   public InterfaceTest(){}
48   
49   // Doesn’t care about type, so new types
50   // added to the system still work right:
51   static void tune(Instrument i) {
52     // ...
53     i.play(9);
54   }
55   static void tuneAll(Instrument[] e) {
56     for(int k = 0; k < e.length; k++) {
57       Instrument i = e[k];
58       tune(i);
59     }
60   }
61   public static void main(String[] args) {
62     // Upcasting during addition to the array:
63     Instrument.VALUE=5;
64     Instrument[] orchestra = new Instrument[5];
65     orchestra[0] = new Wind();
66     orchestra[1] = new Percussion();
67     orchestra[2] = new Stringed();
68     orchestra[3] = new Brass();
69     orchestra[4] = new Woodwind();
70     tuneAll(orchestra);
71   }
72 } /* Output:
73 Wind.play() MIDDLE_C
74 Percussion.play() MIDDLE_C
75 Stringed.play() MIDDLE_C
76 Wind.play() MIDDLE_C  //Brass.play() MIDDLE_C
77 Wind.play() MIDDLE_C  //Woodwind.play() MIDDLE_C
78 *///:~