// examples of using MusicalInstrument family of classes public class TestHarness { // create orchestra and have them play public static void main(String[] args) { // illegal! // MusicalInstrument m = new MusicalInstrument(); // WoodWind w = new Woodwind(); // Percussion p = new Percussion(); Clarinet c = new Clarinet("E flat", "#2"); // legal to use WoodWind variable, if pointing at subclass instance WoodWind w = c; // legal to use Percussion variable, if pointing at subclass instance Percussion p = new Timpani(); // new here gets space for array, doesn't create MusicalInstrument object MusicalInstrument [ ] orchestra = new MusicalInstrument[10]; orchestra[0] = c; // fill array with randomly chosen instruments for (int i = 1; i < orchestra.length; i++) { switch ((int)(Math.random() * 4)) { case 0: orchestra[i] = new Clarinet(); break; case 1: orchestra[i] = new Saxophone(); break; case 2: orchestra[i] = new Glockenspiel(); break; default: orchestra[i] = new Timpani(); } } // display behavior for all elements in array for (int i = 0; i < orchestra.length; i++) { System.out.println("Solo from " + orchestra[i]); orchestra[i].prepare(); orchestra[i].play(); } } }