<< 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.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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