In the 6th chapter, it is known that an object can be used as its own type, or as an object of its underlying type. The behavior used to get an object handle and use it as the underlying type handle is called "tracing"-because the drawing of the inheritance tree is the base class at the top.
However, this will also encounter a problem, as shown in the following example (if you are having trouble executing this program, refer to the 3.1.2 section of chapter 3rd "assignment"):
: Music.java
//Inheritance & upcasting
package c07;
Class Note {
private int value;
Private note (int val) {value = val;}
public static final Note
Middlec = new Note (0),
cSharp = new Note (1),
Cflat = new (2);
}//ETC.
class Instrument {public
void Play (note N) {
System.out.println ("Instrument.play ()");
}
Wind objects are instruments
//because they have the same interface:
class Wind extends instrument {
// Redefine interface method: public
void Play (note N) {
System.out.println ("Wind.play ()");
}
public class Music {public
static void tune (instrument i) {
//...
I.play (Note.middlec);
}
public static void Main (string[] args) {wind
flute = new Wind ();
Tune (flute); Upcasting
}
}///:~
Where the method Music.tune () receives a instrument handle and also receives everything derived from instrument. This occurs when a wind handle is passed to tune (). There is no need for modelling at this time. This is acceptable; the interface in the instrument must exist in the wind, because wind is inherited from the instrument. The trace from wind to instrument may "shrink" the interface, but it is impossible to make it smaller than the full interface of the instrument.