This page is optimized for mobile devices, if you would prefer the desktop version just click here

3.11 Java3010: indirection, array objects, and casting  (Page 4/5)

And those contents are...

That element contains a reference to an object of the class Prob05MyClassA (see Listing 1 ) .

Therefore, a reference to an object of type Prob05MyClassA is passed as a parameter to the method named getDataFromObj .

The class named Prob05MyClassB

It is time to take a look at the class in which the getDataFromObj method is defined.

The class named Prob05MyClassB is shown in its entirety in Listing 4 .

Listing 4 . The class named Prob05MyClassB.
class Prob05MyClassB{ Prob05MyClassB(){System.out.println("Baldwin"); }//end constructorpublic int getDataFromObj(Object refToObj){ return ((Prob05MyClassA)refToObj).getData();}//end getDataFromObj() }//end class Prob05MyClassB

Extends the Object class

Note that this class does not extend the class named Prob05 . In fact, it doesn't explicitly extend any class. This means that it extends the classnamed Object by default because every class is a subclass of the class named Object .

The constructor

The constructor for this class is inconsequential. It simply displays my last name when the object is instantiated, producing part of the output text shown in Figure 1 .

The getDataFromObj method

The interesting part of Listing 4 is the definition of the method named getDataFromObj .

As we saw before, this method receives a reference to an object of type Prob05MyClassA (see Listing 3 ) . However, this reference is not received as the true type of the object. Instead, it isreceived as type Object , which is the ultimate superclass of the class named Prob05MyClassA .

The objective of the method

The objective is to call the method named getData on the incoming reference. However, the Object class doesn't know anything about a method named getData because the Object class neither defines nor inherits a method having that signature. Instead, the getData method is defined in the class named Prob05MyClassA , which is the true type of the object.

A cast is required

Therefore, it is necessary to convert the type of the reference back to its true type using a cast operator before that reference can be used to call themethod named getData . (The cast operator is shown in Listing 4 .)

The returned value

The getData method returns a copy of the value that was passed as a constructor parameter when the object was instantiated. (See Listing 2 .) Recall that the value was the original random value. (See Listing 1 .)

Referring back to Listing 4 , that is the value that is returned from the call to the getDataFromObj method in Listing 3 , which cause the value to be displayed as the first numeric value in Figure 1 .

The end of the main method

Returning once more to the main method and picking up where we left off in Listing 3 , Listing 5 shows the final statement in the main method.

Listing 5 . The end of the main method.
System.out.println(randomNumber); }//end main}//end class Prob05

This statement simply displays the original random value that was passed to the constructor for the Prob05MyClassA in Listing 1 . This statement displays the second numeric value shown as the last line of text in Figure 1 .

<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.