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

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