44 lines
954 B
Java
44 lines
954 B
Java
//: polymorphism/music/Music2.java
|
|
// ©2015 MindView LLC: see Copyright.txt
|
|
// Overloading instead of upcasting.
|
|
package polymorphism.music;
|
|
import static net.mindview.util.Print.*;
|
|
|
|
class Stringed extends Instrument {
|
|
@Override
|
|
public void play(Note n) {
|
|
print("Stringed.play() " + n);
|
|
}
|
|
}
|
|
|
|
class Brass extends Instrument {
|
|
@Override
|
|
public void play(Note n) {
|
|
print("Brass.play() " + n);
|
|
}
|
|
}
|
|
|
|
public class Music2 {
|
|
public static void tune(Wind i) {
|
|
i.play(Note.MIDDLE_C);
|
|
}
|
|
public static void tune(Stringed i) {
|
|
i.play(Note.MIDDLE_C);
|
|
}
|
|
public static void tune(Brass i) {
|
|
i.play(Note.MIDDLE_C);
|
|
}
|
|
public static void main(String[] args) {
|
|
Wind flute = new Wind();
|
|
Stringed violin = new Stringed();
|
|
Brass frenchHorn = new Brass();
|
|
tune(flute); // No upcasting
|
|
tune(violin);
|
|
tune(frenchHorn);
|
|
}
|
|
} /* Output:
|
|
Wind.play() MIDDLE_C
|
|
Stringed.play() MIDDLE_C
|
|
Brass.play() MIDDLE_C
|
|
*///:~
|