<< Chapter < Page Chapter >> Page >

Thus, it is possible to define an anonymous class that extends a class whose constructor requires parameters, and to pass those parameters to the superclassconstructor when the anonymous class is instantiated.

The parentheses following interfaceName in the second expression in Figure 1 must always be empty. In this case, the superclass is always Object , which never expects constructor parameters.

Discussion and sample code

The paragraphs that follow will explain a program named InnerClasses08 . This program is designed specifically to illustrate anonymous classes, and to compareanonymous classes with local classes.

I will discuss the program in fragments. A complete listing of the program is provided in Listing 10 .

When the program is executed, it produces the GUI shown in Figure 2 . I will refer back to this image during the discussion of the program.

Class file names

This program consists of a total of six classes:

  • Two top-level classes
    • InnerClasses08.class
    • GUI.class
  • One local class
    • GUI$1$BaldButton.class
  • Three anonymous classes
    • GUI$1.class
    • GUI$2.class
    • GUI$3.class

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

(As you can see, the anonymous classes are not truly anonymous, since the files that represent them must have names. Generally, however, theestablishment of the individual names is beyond the control of the programmer, and the names are not known to the program in a way that makesit possible to refer to them by name.)

Program structure and behavior

This program is designed to illustrate the use of local classes and anonymous classes in a very practical way. Itillustrates one implementation of a local class and three different implementations of anonymous classes. The program compares the local class withan anonymous class designed to accomplish the same purpose. The program also illustrates the use of instance initializers as an alternative toconstructors.

A local class

The program defines and uses a local class to instantiate an object to handle mouse clicked events on a button with low-level event handling. This class uses a constructor to enable mouse events on a newextended Button class. It also uses a constructor to display the name of the class file.

Three anonymous classes

An anonymous class to compare with the local class

The program also defines and uses an anonymous class to instantiate an object tohandle mouse clicked events on a button with low-level event handling.

This class uses an instance initializer to enable mouse events on a new extended Button class. It also uses an instance initializer to display the name of the class file.

This class and the local class described above provide a direct comparison between the use of local classes and anonymous classes toserve the same purpose.

An anonymous class that implements an interface

The program illustrates the use of an anonymous class that implements the MouseListener interface, to instantiate an object to handle mouse clicked events using the source-listener event model (sometimes referred to as the delegation event model or the JavaBeans event model). The anonymous class uses an instance initializer to display the name of the class file.

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