14

This code snippet for the interface SetObserver is taken from Effective Java (Avoid Excessive Synchronization Item 67)

public interface SetObserver<E> {
// Invoked when an element is added to the observable set
void added(ObservableSet<E> set, E element);
}

And the SetObserver is passed to addObserver() and removeObserver method as given below :

// Broken - invokes alien method from synchronized block!
public class ObservableSet<E> extends ForwardingSet<E> {
  public ObservableSet(Set<E> set) {
    super(set);
  }

  private final List<SetObserver<E>> observers =
      new ArrayList<SetObserver<E>>();

  public void addObserver(SetObserver<E> observer) {
    synchronized (observers) {
      observers.add(observer);
    }
  }



  public boolean removeObserver(SetObserver<E> observer) {
    synchronized (observers) {
      return observers.remove(observer);
    }
  }



  private void notifyElementAdded(E element) {
    synchronized (observers) {
      for (SetObserver<E> observer : observers)
        observer.added(this, element);
    }
  }

Bloch refers to the SetObserver<E> interface as a call back interface . When is a interface called an call back interface in Java?

Geek
  • 26,489
  • 43
  • 149
  • 227
  • 1
    possible duplicate of [What is a callback function?](http://stackoverflow.com/questions/824234/what-is-a-callback-function) – assylias Jul 20 '12 at 10:42
  • 1
    `ObservableSet` is not an interface, I guess you mean `SetObserver` – Alonso Dominguez Jul 20 '12 at 10:46
  • @AlonsoDominguez yes it was a typo. editing the question right now . – Geek Jul 20 '12 at 10:47
  • An interface with callback functions! – Ozair Kafray Jul 20 '12 at 10:48
  • @OzairKafray then every interface in java is a callback interface ? – Geek Jul 20 '12 at 10:49
  • @Geek I don't think there is anything special about callback interfaces. There is no special way of defining or treating them. Its just a phrase. And its the only way of implementing callbacks in Java, I believe. Read about Callbacks in general and then about callbacks in java. – Ozair Kafray Jul 20 '12 at 10:52
  • @OzairKafray I know the term "callbacks" are often used to describe functions in javascript that respond to a browser event but I do not think I ever heard of a callback in context of Java . Hence I posted this question. – Geek Jul 20 '12 at 10:54
  • 2
    @Geek Callback is a very general term used in computer science. When a piece of code is interested in an event, it registers a function to be called when an event happens, so that this piece of code can do what is required. It is not specific to javascript. In C/C++ there were function pointers, in C# there are delegates and in Java we do it using interfaces. – Ozair Kafray Jul 20 '12 at 10:57
  • 1
    Interfaces can be used to implement *callbacks* in Java. A *callback* is a situation where you'd like to pass a **reference** to some behavior and have another object invoke it later. In C or C++, this is prime territory for function pointers. In Java, we use *interfaces* instead. – Lion Jul 20 '12 at 11:08

1 Answers1

19

A general requirement for an interface to be a "callback interface" is that the interface provides a way for the callee to invoke the code inside the caller. The main idea is that the caller has a piece of code that needs to be executed when something happens in the code of another component. Callback interfaces provide a way to pass this code to the component being called: the caller implements an interface, and the callee invokes one of its methods.

The callback mechanism may be implemented differently in different languages: C# has delegates and events in addition to callback interfaces, C has functions that can be passed by pointer, Objective C has delegate protocols, and so on. But the main idea is always the same: the caller passes a piece of code to be called upon occurrence of a certain event.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I may be wrong, but in Java's case wouldn't it be correct to say the callee implements the interface so that the caller can call it back and not the other way around? Sorry, I know this answer is almost 3 years old. – stevenelberger Mar 17 '15 at 07:02
  • 1
    @stevenelberger I think the current description is right: caller implements an interface, gives that implementation to the callee, and callee calls back methods of the implementation as needed. – Sergey Kalinichenko Mar 17 '15 at 10:01