<< Chapter < Page Chapter >> Page >

The definition and instantiation of an anonymous class uses one or the other of the two expressions shown in Figure 1 .

Usually, this expression is included inside a larger overall expression, such as an argument to a method call.

What does the first expression mean?

Here is how I usually explain this syntax to my students. The first expression in Figure 1 starts out fairly normal, but becomes cryptic very quickly.

This expression instantiates a new object from an unnamed and previously undefined class. That class automatically extends the class named className , and cannot explicitly implement any interfaces.

The body of the new class is given by classBody .

The result of executing this expression is that:

  • a new class that extends className is defined,
  • a new object of the new class is instantiated, and
  • the expression is replaced by a reference to the new object.

Example usage

If this expression appears as the right operand of an assignment operator, the new object's reference is saved inthe left operand of the assignment operator.

If the expression appears as an argument in a method call, the new object's reference is passed to themethod.

If the expression appears in some other form of larger overall expression, the new object's reference is handed over to the surrounding expressionto be used appropriately.

What about instantiating an interface?

The second expression in Figure 1 starts out very weird. To my knowledge, there is no other situation in Java where you apply the new operator to the name of an interface.

From the beginning, you have been told (or should have been told) that you cannot instantiate an object of an interface. (An interface is implicitly abstract and it doesn't have a constructor, not even a default constructor.)

However, you can instantiate an object of a class that implements none, one, or more interfaces.

The correct interpretation of the second expression in Figure 1 is as follows. This expression instantiates a new object from anunnamed and previously undefined class. That class automatically implements the interface named interfaceName , and it automatically extends the class named Object .

The class can explicitly implement one, and only one interface, and cannot extend any class other than Object .

Once again, the body of the new class is given by classBody .

As in the case of the first expression in Figure 1 , the result of executing this second expressionis that

  • a new class that implements interfaceName is defined,
  • a new object of the new class is instantiated, and
  • the expression is replaced by a reference to the new object.

That reference is handed over to the surrounding expression to be used appropriately.

What about constructor parameters?

As mentioned earlier in this module, since the new class doesn't have a name, it isn't possible to define a constructor for the new class.

According to Flanagan (with regard to the first expression in Figure 1 ) ,

"Any arguments you specify between the parentheses following the superclass name in an anonymous class definition are implicitly passed tothe superclass constructor."

Questions & Answers

the Three stages of Auguste Comte
Clementina Reply
what are agents of socialization
Antonio Reply
sociology of education
Nuhu Reply
definition of sociology of education
Nuhu
what is culture
Abdulrahim Reply
shared beliefs, values, and practices
AI-Robot
What are the two type of scientific method
ogunniran Reply
I'm willing to join you
Aceng Reply
what are the scientific method of sociology
Man
what is socialization
ogunniran Reply
the process wherein people come to understand societal norms and expectations, to accept society's beliefs, and to be aware of societal values
AI-Robot
scientific method in doing research
ogunniran
defimition of sickness in afica
Anita
Cosmology
ogunniran
Hmmm
ogunniran
list and explain the terms that found in society
REMMY Reply
list and explain the terms that found in society
Mukhtar
what are the agents of socialization
Antonio
Family Peer group Institution
Abdulwajud
I mean the definition
Antonio
ways of perceived deviance indifferent society
Naomi Reply
reasons of joining groups
SAM
to bring development to the nation at large
Hyellafiya
entails of consultative and consensus building from others
Gadama
World first Sociologist?
Abu
What is evolutionary model
Muhammad Reply
Evolution models refer to mathematical and computational representations of the processes involved in biological evolution. These models aim to simulate and understand how species change over time through mechanisms such as natural selection, genetic drift, and mutation. Evolutionary models can be u
faruk
what are the modern trends in religious behaviours
Selekeye Reply
what are social norms
Daniel Reply
shared standards of acceptable behavior by the group or appropriate behavior in a particular institution or those behaviors that are acceptable in a society
Lucius
that is how i understood it
Lucius
examples of societal norms
Diamond
Discuss the characteristics of the research located within positivist and the interpretivist paradigm
Tariro Reply
what is Industrialisation
Selekeye Reply
industrialization
Angelo
With real life examples , describe any four importance of sociology that help in academic achievement
Lazarus Reply
summarize halerambos & holbon
David 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