<< 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 does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
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