Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.12. Anonymous classes

4.12. Anonymous classes
Last updated: 23 January 2013.

An anonymous class is a class without a name that is used in the body of a method. In some situations, you need an instance of a class that is used only once. In such cases, giving a variable name to the class is not necessary. An anonymous class is an expression that can be part of a larger expression. For example, it can be passed in as an argument to a method call. To create an anonymous class, you can use the keyword new in two different ways as shown below:

1. You can instantiate a class and optionally override its methods:

new <ClassName> ( [ <constructor-arguments> ] ) { <override-methods-here> }

2. You can also implement an interface:

new <InterfaceName> () { <implement-methods-here> }



As an example to illustrate the second way of creating an anonymous class, let's get back to the interface defined in the tutorial interfaces:

public interface AircraftInterface {

   
int MINIMUM_TAKEOFF_DISTANCE = 0;
   
int MAXIMUM_CREW_COUNT = 30;

   
public boolean land();
   
public boolean takeOff();

}

Let's assume that your application has a class named AircraftTesting which contains a method named testAircraft that takes an argument of type AircraftInterface:

public final class AircraftTesting {

   
public void testAircraft(AircraftInterface aircraft){
         
// run the tests...
   
}

}

In the example shown below, I am passing to the method testAircraft an anonymous class that implements the interface AircraftInterface:

AircraftTesting testing = new AircraftTesting();

testing.testAircraft
(new AircraftInterface(){
   
public boolean land(){
         
// implement the method land here
   
}
   
public boolean takeOff(){
         
// implement the method takeOff here
   
}
})
;

At first glance, the syntax may be a bit confusing. In fact, the code above can be seen as follows:

AircraftTesting testing = new AircraftTesting();

testing.testAircraft
( myAnonymousClassExpression );

In the code above, myAnonymousClassExpression is the expression shown below:

new AircraftInterface(){
   
public boolean land(){
         
// implement the method land here
   
}
   
public boolean takeOff(){
         
// implement the method takeOff here
   
}
}

As you can see, I used the keyword new instead of the keyword implements to implement the interface and thereby create the anonymous class. You can use the keyword new followed by an interface name only when creating an anonymous class. In that case, the newly created anonymous class is a direct subclass of the class Object.

Instead of implementing an interface, you can instantiate a class as described in the first example of this tutorial. In that case, the created anonymous class is a subclass of the class whose name was used after the keyword new.

Note that an anonymous class cannot define a constructor since it does not have a name. Moreover, an anonymous class cannot define static members.

Anonymous classes are often used to implement graphical user interface listeners as you will see in the next tutorials.


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.12. Anonymous classes
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.13. Object type casting

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7