2.6 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( ... |
Get Programming Groovy 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.