The most noteworthy part of inheritance is that it does not provide a method for the new class. Inheritance is an expression of the relationship between a new class and the underlying class. You can summarize the relationship by saying that the new class belongs to a type of an existing class.
This expression is not only a kind of visualization interpretation of inheritance, which is directly supported by language. As an example, consider a basic class called instrument, which is used to represent musical instruments, and another derived class called Wind. Since inheritance means that all the methods of the underlying class can also be used in derived classes, any message we send to the underlying class can also be sent to the derived class. If the instrument class has a play () method, then wind equipment will also have this method. This means that we can be sure that a wind object is also a type of instrument. The following example shows how the compiler provides support for this concept:
: Wind.java
//Inheritance & upcasting
import java.util.*;
Class Instrument {public
void Play () {}
static void tune (instrument i) {
//...
I.play ();
}
Wind objects are instruments
//because they have the same interface:
class Wind extends instrument {
PU Blic static void Main (string[] args) {wind
flute = new Wind ();
Instrument.tune (flute); Upcasting
}
}///:~
The most interesting of this example is undoubtedly the tune () method, which accepts a instrument handle. In Wind.main (), however, the tune () method is invoked by assigning a wind handle to it. Because Java is particularly strict with type checking, you may find it strange why receiving one type of method can also receive another type. However, we must realize that a wind object is also a instrument object. and for a instrument (musical instrument) that is not in wind, no method can be invoked by tune (). In tune (), the Code applies to instrument and anything derived from instrument. Here, the behavior of converting from a wind handle to a instrument handle is called "Tracing the shape".