Providing Callbacks via Interfaces
Problem
You want to provide callbacks ; that is, have unrelated classes call back into your code.
Solution
One way is to use a Java interface.
Discussion
An interface is a class-like object that can contain only abstract methods and final fields. As we’ve seen, interfaces are used a lot in Java! In the standard API, the following are a few of the commonly used interfaces:
Runnable
,Comparable
, andCloneable
(injava.lang
)List
,Set
,Map
, andEnumeration/Iterator
(in the Collections API; see Chapter 7)ActionListener
,WindowListener
, and others (in the AWT GUI; see Section 13.5)Driver, Connection, Statement
, andResultSet
(in JDBC; see Section 20.4)The "remote interface” -- the contact between the client and the server -- is specified as an
Interface
(in RMI, CORBA, and EJB)
Get Java Cookbook 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.