<< Chapter < Page Chapter >> Page >

The screen output

The combined code in Listing 11 and Listing 12 produces the output shown in Figure 5 for the first object instantiated from local class B .

Call private method showA

One of the important characteristics of local classes is that the methods of objects instantiated from local classeshave direct access to all of the members, including private members, of all the containing classes in the containment hierarchy.

(In this case, an object of the local class B has only one containing class, an object instantiated from the top-level class A .)

Continuing with the method named showB , in local class B , the code in Listing 13 displays a line separator, -2-, and then calls the private method named showA on the containing object. This illustrates the containment hierarchy.

The screen output

The method named showA is shown in Listing 5 . The combination of the code in Listing 13 and Listing 5 produces the screenoutput shown in Figure 6 .

Figure 6 displays

  • The relative time that the containing object of the class A was instantiated
  • A protected instance variable in the superclass of the class from which the containing object was instantiated
  • The name of the class file that represents the top-level class named A

Use of the keyword this

As is the case with member classes, (discussed in the previous module), objects of local classes use a special syntax of the keyword this to gain access to objects in the containment hierarchy.

The code in Listing 14 shows how to use regular syntax to gain access to the current object, and how to use special syntax to gain access to thecontaining object.

Having gained access to the two objects, the code in Listing 14 gets and displays the values of private instance variables belonging to those objects.

The screen output

The code in Listing 14 produced the screen output shown in Figure 7 .

You saw these same two values displayed earlier in Figure 4 and Figure 2 .

Illustrate the inheritance hierarchy

Remember that the inheritance hierarchy is independent of the containment hierarchy. The code in Listing 15 illustrates the inheritance hierarchy by

  • Calling the overridden toString method belonging to the local class B
  • Using the super keyword to call the overridden toString method belonging to the class Y , which is the superclass of the local class B

Listing 15 also defines the overridden toString method belonging to local class B . The overridden toString method belonging to class Y is shown in Listing 3 .

The screen output

The combined code in Listing 15 and Listing 3 produces the screen output shown in Figure 8 . (Once again, you have seen these same values displayed in earlier Figures.)

That concludes the results of calling the showB method on the first object instantiated from local class B .

Call showB method on second object

The code in Listing 10 instantiated two objects from local class B , and saved the object's references in instance variables named obj1 and obj2 . The screen output that I have been discussing resulted from calling the showB method on obj1 in Listing 11 .

Listing 16 calls the showB method on obj2 . Listing 16 also signals the end of the method named meth , and the end of the class named A .

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