<< Chapter < Page Chapter >> Page >
(Note that while the containment hierarchy of local classes is independent of the inheritance hierarchy, it is technically possible toestablish an inheritance relationship between a local class and one of its enclosing classes. For example, by making a couple of minor modifications,it is possible to cause the local class B in this program to extend the enclosing class A instead of the top-level class Y .)

The controlling class

The entire controlling class named InnerClasses07 , including the main method and a static variable named baseTime , is shown in Listing 1 .

The static variable named baseTime is initialized with the current date and time in milliseconds (relative to January 1, 1970). This static variable is used later as a base to establish the relative points in time thatcertain activities occur during the execution of the program.

The main method

The main method shown in Listing 1 instantiates a new object of the class named A and calls the method named meth on that object.

This method will sequentially instantiate two separate objects of a local class named B that is defined inside the method named meth . Then the method named meth will call a method named showB on each of those objects, causing them to display data values that illustrate the characteristicsof local classes.

The class named X

The top-level class named X is shown in Listing 2 .

The class named A extends this class to illustrate the difference between the inheritance hierarchy and the containment hierarchy. Note that thisclass defines and initializes a protected instance variable, which will be accessed later to illustrate the inheritance hierarchy.

(Somewhat surprisingly, you will also see later that this protected instance variable belonging to the superclass of the enclosing class A is also accessible by methods belonging to an object of the local class viathe containment hierarchy.)

The class named Y

The top-level class named Y is shown in Listing 3 .

The local class named B extends this class to illustrate the difference between the inheritance hierarchy and the containment hierarchy.

Note that this class defines and initializes a protected instance variable. It alsooverrides the toString method, which is inherited from the Object class.

The instance variable and the overridden toString method will be accessed later to illustrate the inheritance hierarchy.

The class named A

The code in Listing 4 shows the beginning of the top-level class named A .

This code declares and initializes a private instance variable named aTime , which establishes the relative time that an object of class A is instantiated.

This private instance variable will be accessed directly by code belonging to an object of the local class B , which is contained by an object of the class A .

Listing 4 also shows the constructor for class A , which displays the time whenever an object is instantiated from class A .

The screen output

The code shown earlier in the main method of Listing 1 instantiates an object of class A . This causes the screen output shown in Figure 2 .

(Note that the relative time output value on your system may be different, depending on the speed of your system and the impact of otherapplications that may be running concurrently.)

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