<< Chapter < Page Chapter >> Page >

This program illustrates the use of member classes. The program consists of a total of six classes:

  • Top-level classes named
    • InnerClasses06
    • A
    • X
  • Member classes named
    • B
    • C
    • D

When compiled, the program produces the class files shown in Figure 1 .

Class containment hierarchy

Once you understand the class file naming convention, you can determine from the file names in Figure 1 that class B is a member class of class A.

(The class file named A$B.class indicates that the class named B is a member of the class named A.)

Similarly, class C is a member of class B, and class D is a private member of class C.

(However, you cannot tell from the class file names that class D is private.)

Behavior of the program

An object is instantiated from the class named A. This makes it possible to instantiate an object ofthe member class named B. The object of the class named B is internally linked to the object of the class named A.

(This causes the instance variable, constructor parameter, and accessor methods discussed above to be automatically created to link the object ofthe class named B to the object of the class named A.)

The object of the class named B is used to instantiate an object of the member class named C. This object of the class C is linked to the object ofthe class named B.

Instantiate additional objects of classes A and B, plus an object of class D

When the object of the class named C is instantiated, the constructor for that class instantiates separate objects of the classes named Aand B, and also instantiates an object of the private member class named D.

(We will see later that the new and separate object of the class named B continues to be internally linked to the original object of the Class namedA, and is not internally linked to the new object of the class named A.)

Instantiation of the object of class D illustrates the use of private member classes.

(Note that while top-level classes cannot be private, member classes can be private.)

Perform a variety of operations

A variety of operations are performed from within the methods belonging to the object of the class C to illustrate theattributes and behavior of objects instantiated from member classes.

Comments in the code explain the purpose of each of those operations.

Many of those operations produce screen output, which will be shown in conjunction with thecode that produced the output.

Structure of the program

The main method

The main method of the controlling class named InnerClasses06 , is shown in Listing 1 .

The code in Listing 1 instantiates an object of the member class named C and calls the method named cShow on that object.

(Note that it is necessary to first instantiate objects of the enclosing classes named A and B before the object of the member class named C can beinstantiated.)

An independent top-level class named X

Listing 2 shows the definition of an independent top-level class named X.

This class will be extended by the class named C, which is a member of the class named B, which is a member of the class named A. This willillustrate that the inheritance hierarchy is independent of the containment hierarchy.

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