Now let's return to the musical instrument (instrument) example. Because of its polymorphism, you can add as many new types as you want to the system without changing the True () method. In a well-designed OOP program, most or all of our methods comply with the tune () model and communicate only with the underlying class interface. We say such programs have "extensibility" because new data types can be inherited from common base classes, adding new functionality. If it is to accommodate the requirements of the new class, then the method of manipulating the underlying class interface does not need to change at all.
For an example of a musical instrument, suppose we add more methods to the underlying class and a series of new classes, what will happen? Here is the schematic:
All these new classes work with the old class--tune () without any adjustment to tune (). Even if tune () is in a separate file, and the new method is added to the Instrument interface, tune () works correctly and does not need to be recompiled. The following procedure is a concrete implementation of the above schematic:
: Music3.java//an extensible import java.util.*;
Class Instrument3 {public void play () {System.out.println ("Instrument3.play ()");
Public String What () {return "Instrument3"; The public void adjust () {}} class Wind3 extends Instrument3 {public void play () {System.out.println ("Wind3.play
()");
Public String What () {return "Wind3";} public void adjust () {}} class Percussion3 extends Instrument3 {public void play () {System.out.println ("Percussi
On3.play () ");
Public String What () {return "Percussion3";} public void adjust () {}} class Stringed3 extends Instrument3 {public void play () {System.out.println ("Stringed3.
Play () ");
Public String What () {return "Stringed3";}
public void adjust () {}} class Brass3 extends Wind3 {public void play () {System.out.println ("Brass3.play ()");
public void adjust () {System.out.println ("brass3.adjust ()"); } class Woodwind3 extends Wind3 {public void Play () {System.out.println ("Woodwind3.play ()");
Public String What () {return ' Woodwind3 ';}} public class Music3 {//doesn ' t care about type, so new types//Added to the system still work right:static void
Tune (Instrument3 i) {//... i.play ();
} static void Tuneall (instrument3[] e) {for (int i = 0; i < e.length; i++) tune (e[i));
public static void Main (string[] args) {instrument3[] Orchestra = new INSTRUMENT3[5];
int i = 0;
upcasting during addition to the array:orchestra[i++] = new Wind3 ();
orchestra[i++] = new Percussion3 ();
orchestra[i++] = new Stringed3 ();
orchestra[i++] = new BRASS3 ();
orchestra[i++] = new Woodwind3 ();
Tuneall (orchestra); }
} ///:~
The new method is what () and adjust (). The former returns a string handle and returns a description of that class, which allows us to adjust each instrument.
In main (), when we put something into the Instrument3 array, we automatically trace the shape to the Instrument3.
As you can see, while all the other code around the tune () method changes, the tune () method is not affected by them and remains intact. This is what the use of polymorphism is intended to achieve. The changes we make to the code do not affect the part of the program that should not be affected. In addition, we believe that polymorphism is a crucial technique that allows programmers to "separate what is changed from what is not changed".