O'Reilly logo

Programming Groovy 2 by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing Interfaces

In Groovy we can morph a map or a block of code into interfaces, which lets us implement interfaces with multiple methods quickly. In this section, you’ll see a Java way of implementing interfaces, and then you’ll learn how to take advantage of Groovy’s facilities.

Here’s the all-too-familiar Java code to register an event handler to a Swing JButton. The call to addActionListener expects an instance that implements the ActionListener interface. We create an anonymous inner class that implements ActionListener, and we provide the required actionPerformed method. This method insists on taking ActionEvent as an argument even though we have no use for it in this example.

 
// Java code
 
button.addActionListener(​new

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required