22 lines
534 B
Java
22 lines
534 B
Java
// polymorphism/music/Music.java
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://OnJava8.com for more book information.
|
|
// Inheritance & upcasting
|
|
// {java polymorphism.music.Music}
|
|
package polymorphism.music;
|
|
|
|
public class Music {
|
|
public static void tune(Instrument i) {
|
|
// ...
|
|
i.play(Note.MIDDLE_C);
|
|
}
|
|
public static void main(String[] args) {
|
|
Wind flute = new Wind();
|
|
tune(flute); // Upcasting
|
|
}
|
|
}
|
|
/* Output:
|
|
Wind.play() MIDDLE_C
|
|
*/
|