Figure 14.14 // // This class is used by the <Java1407> program. public class Train { ArrayList<TrainCar> trainCars; private int tlX; private int tlY; public Train(int tlX, int tlY) { trainCars = new ArrayList<TrainCar>(); this.tlX = tlX; this.tlY = tlY; trainCars.add(new Locomotive(Color.red,tlX,tlY)); trainCars.add(new TrainCar(Color.green,tlX+160,tlY)); trainCars.add(new TrainCar(Color.yellow,tlX+320,tlY)); trainCars.add(new trainCars.add(new Caboose(Color.blue,tlX+640,tlY)); } public void drawTrain(Graphics g) { for (TrainCar tc: trainCars) tc.drawCar(g); } } Let us assume that you understand the logic of the TrainCar, Locomotive and Caboose classes. Each one of these classes constructs a new object and also defines the drawCar method. In the case of the superclass TrainCar, method drawCar has its first appearance and method drawCar is then re-defined by the Locomotive and Caboose classes. Everything that was stated is required for polymorphism, but it only sets the stage. The essence of polymorphism now shows up in a class where objects are used that all fall under the same umbrella class, but these objects have each been constructed according to the constructor of their own class. Additionally, each object is called upon to process some method that all classes have in common by name, but not by action. So let's analyze the figure 4.14 Train class. A dynamic generic called trainCars is defined and every member is a TrainCar object. That is step one. We have the umbrella class. Now in step two each object is added to the array. These objects are constructed with three different constructors, but Java does not object. The array requires TrainCar objects and TrainCar objects are precisely what you add.
Figure 14.14 // // This class is used by the <Java1407> program. public class Train { ArrayList<TrainCar> trainCars; private int tlX; private int tlY; public Train(int tlX, int tlY) { trainCars = new ArrayList<TrainCar>(); this.tlX = tlX; this.tlY = tlY; trainCars.add(new Locomotive(Color.red,tlX,tlY)); trainCars.add(new TrainCar(Color.green,tlX+160,tlY)); trainCars.add(new TrainCar(Color.yellow,tlX+320,tlY)); trainCars.add(new trainCars.add(new Caboose(Color.blue,tlX+640,tlY)); } public void drawTrain(Graphics g) { for (TrainCar tc: trainCars) tc.drawCar(g); } } Let us assume that you understand the logic of the TrainCar, Locomotive and Caboose classes. Each one of these classes constructs a new object and also defines the drawCar method. In the case of the superclass TrainCar, method drawCar has its first appearance and method drawCar is then re-defined by the Locomotive and Caboose classes. Everything that was stated is required for polymorphism, but it only sets the stage. The essence of polymorphism now shows up in a class where objects are used that all fall under the same umbrella class, but these objects have each been constructed according to the constructor of their own class. Additionally, each object is called upon to process some method that all classes have in common by name, but not by action. So let's analyze the figure 4.14 Train class. A dynamic generic called trainCars is defined and every member is a TrainCar object. That is step one. We have the umbrella class. Now in step two each object is added to the array. These objects are constructed with three different constructors, but Java does not object. The array requires TrainCar objects and TrainCar objects are precisely what you add.
Text14-PolymorphismAbstractness.docx