2015-09-07 11:44:36 -06:00
|
|
|
// interfaces/music4/Music4.java
|
2020-10-07 13:35:40 -06:00
|
|
|
// (c)2020 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Abstract classes and methods
|
2016-07-28 12:48:23 -06:00
|
|
|
// {java interfaces.music4.Music4}
|
2015-06-15 17:47:35 -07:00
|
|
|
package interfaces.music4;
|
|
|
|
import polymorphism.music.Note;
|
|
|
|
|
|
|
|
abstract class Instrument {
|
|
|
|
private int i; // Storage allocated for each
|
|
|
|
public abstract void play(Note n);
|
|
|
|
public String what() { return "Instrument"; }
|
|
|
|
public abstract void adjust();
|
|
|
|
}
|
|
|
|
|
|
|
|
class Wind extends Instrument {
|
|
|
|
@Override
|
|
|
|
public void play(Note n) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Wind.play() " + n);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String what() { return "Wind"; }
|
|
|
|
@Override
|
2015-12-02 09:20:27 -08:00
|
|
|
public void adjust() {
|
|
|
|
System.out.println("Adjusting Wind");
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class Percussion extends Instrument {
|
|
|
|
@Override
|
|
|
|
public void play(Note n) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Percussion.play() " + n);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String what() { return "Percussion"; }
|
|
|
|
@Override
|
2015-12-02 09:20:27 -08:00
|
|
|
public void adjust() {
|
|
|
|
System.out.println("Adjusting Percussion");
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class Stringed extends Instrument {
|
|
|
|
@Override
|
|
|
|
public void play(Note n) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Stringed.play() " + n);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String what() { return "Stringed"; }
|
|
|
|
@Override
|
2015-12-02 09:20:27 -08:00
|
|
|
public void adjust() {
|
|
|
|
System.out.println("Adjusting Stringed");
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class Brass extends Wind {
|
|
|
|
@Override
|
|
|
|
public void play(Note n) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Brass.play() " + n);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
@Override
|
2015-12-02 09:20:27 -08:00
|
|
|
public void adjust() {
|
|
|
|
System.out.println("Adjusting Brass");
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class Woodwind extends Wind {
|
|
|
|
@Override
|
|
|
|
public void play(Note n) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("Woodwind.play() " + n);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public String what() { return "Woodwind"; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Music4 {
|
|
|
|
// Doesn't care about type, so new types
|
|
|
|
// added to the system still work right:
|
|
|
|
static void tune(Instrument i) {
|
|
|
|
// ...
|
|
|
|
i.play(Note.MIDDLE_C);
|
|
|
|
}
|
|
|
|
static void tuneAll(Instrument[] e) {
|
|
|
|
for(Instrument i : e)
|
|
|
|
tune(i);
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
// Upcasting during addition to the array:
|
|
|
|
Instrument[] orchestra = {
|
|
|
|
new Wind(),
|
|
|
|
new Percussion(),
|
|
|
|
new Stringed(),
|
|
|
|
new Brass(),
|
|
|
|
new Woodwind()
|
|
|
|
};
|
|
|
|
tuneAll(orchestra);
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2015-06-15 17:47:35 -07:00
|
|
|
Wind.play() MIDDLE_C
|
|
|
|
Percussion.play() MIDDLE_C
|
|
|
|
Stringed.play() MIDDLE_C
|
|
|
|
Brass.play() MIDDLE_C
|
|
|
|
Woodwind.play() MIDDLE_C
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|