ptg7068951
166 HOUR 12:Making the Most of Existing Objects
20: System.out.println(“The 3D point is located at (“+ object2.x
21: + “, “+ object2.y + “, “+ object2.z + “)”);
22: System.out.println(“\tIt’s being moved to (10, 22, 71)”);
23: object2.move(10,22,71);
24: System.out.println(“The 3D point is now at (“+ object2.x
25: + “, “+ object2.y + “, “+ object2.z + “)”);
26: System.out.println(“\tIt’s being moved -20 units on the x, y “
27: + “and z axes”);
28: object2.translate(-20,-20,-20);
29: System.out.println(“The 3D point ends up at (“+ object2.x
30: + “, “+ object2.y + “, “+ object2.z + “)”);
31: }
32: }
When you run the file by choosing Run, Run File, you see the output shown
in Figure 12.3 if the program compiled properly. If not, look for the red icon
alongside the source editor that indicates the line that triggered an error.
LISTING 12.3 Continued
FIGURE 12.3
The output of the PointTester
program.
Summary
When people talk about the miracle of birth, they’re probably not speaking
of the way a superclass in Java can give birth to subclasses or the way
behavior and attributes are inherited in a hierarchy of classes.
If the real world worked the same way that OOPdoes, every descendant of
Mozart could choose to be a brilliant composer. All descendants of Mark
Twain could be poetic about Mississippi riverboat life. Every skill your ances-
tors worked to achieve would be handed to you without an ounce of toil.
On the scale of miracles, inheritance isn’t quite up to par with continuing
the existence of a species or throwing consecutive no-hitters. However, it’s
an effective way to design software with a minimum of redundant work.