<< Chapter < Page Chapter >> Page >

Could cast to the class type

First, it is always possible to cast the reference back to the class from which the object was instantiated. Therefore, it would work to cast thereference from array element 0 in Listing 10 to type Prob05MyClassA and to cast the reference from array element 1 to type Prob05MyClassB .

Cast to the interface type

In this program, there is another choice. Because both classes implement the interface named Prob05X , and the method named getModifiedData is declared in that interface, it also works to cast both references to the commoninterface type Prob05X .

That is what was done in Listing 10 . Both references were cast to the interface type Prob05X .

The printed values

The first statement in Listing 10 calls the method named getModifiedData as defined in Listing 4 . This causes the original random value less 1 to be printed.

The second statement in Listing 10 simply prints the original random value that was saved in the variable named randomData in Listing 1 .

The third statement in Listing 10 calls the method named getModifiedData as defined in Listing 8 . This causes the original random value plus 1 to be printed.

Because this is a call to the println method, the onscreen cursor advances to the left side of the next line after the value is printed.

The three statements in Listing 10 cause the first three values shown in Figure 1 to be printed on the command line screen.

Three more print statements

Continuing with the driver class named Prob05 , Listing 11 shows three more print statements.

Listing 11 . Three more print statements.
System.out.print(((Prob05X)var1[0]).getData() + " ");System.out.print(randomData + " "); System.out.println(((Prob05X)var1[1]).getData());

A cast is required

In this case, the getData method belonging to each of the objects is called in the first and third statements. (Once again a cast is required.)

Behavior of the getData methods is the same

Recall that the behavior of the getData method is the same in both objects. It simply returns a copy of the original random value that was passedto the constructor when each of the objects was instantiated.

The three statements in Listing 11 produce the second setof three matching values shown in Figure 1 .

These three values match because all three print statements are printing essentially the same value. The original random value is printed in the middlestatement in Listing 11 . A copy of the original random value is printed in the first and third statements.

Print the references to the two objects

Things get a little bit more complicated in Listing 12 .

Listing 12 . Print the references to the two objects.
System.out.print(((Prob05X)var1[0]) + " ");System.out.print(randomData + 5 + " "); System.out.println(((Prob05X)var1[1])); }//end main}//end class Prob05

An automatic call to the toString method

Whenever an object's reference is passed to either the print method or the println method, the first thing that happens is that the toString method is called on the reference. The toString method always returns a reference to an object of the String class, and it is that string that is printed.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask