Skip to content Skip to navigation Skip to collection information

OpenStax-CNX

You are here: Home » Content » Object-Oriented Programming (OOP) with Java » Java OOP: Callbacks - I

Navigation

Table of Contents

Recently Viewed

This feature requires Javascript to be enabled.
 

Java OOP: Callbacks - I

Module by: R.G. (Dick) Baldwin. E-mail the author

Summary: Learn the fundamentals of callbacks using interfaces in Java.

Preface

This module is one in a series of three modules designed to teach you about callbacks in Object-Oriented Programming (OOP) using Java. The other two modules are titled Callbacks - II and Callbacks - III. If interested, you will find those modules at http://www.dickbaldwin.com/toc.htm .

Viewing tip

I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the figures and listings while you are reading about them.

Figures

Listings

Preview

Many processes in the standard Java API make use of a mechanism that might be referred to as a callback mechanism. Basically, this is a mechanism where a method in one object asks a method in another object to "call me back" or "notify me" when an interesting event happens.

An interesting event

For example, an interesting event might be that the price of a specified stock goes above its previous high value, or the toaster finishes toasting the bread.

Multicasting

In fact, many different objects may ask one object to notify them when the interesting event happens. This is sometimes referred to as multicasting . (The one-to-one case is often referred to as unicasting.)

Going further, many different objects may ask one object to notify them when any interesting event in a family of interesting events happens, and to identify the specific event that actually happened along with the notification.

Many examples

For example, we see different forms of callback activity in conjunction with

  • the Delegation Event Model used with GUIs in JDK 1.1,
  • the Observer/Observable concept used in the Model-View-Controller paradigm,
  • the concept of Bound Properties and Constrained Properties in Java Beans , etc.

You can find examples of all of these in the pages of my online tutorial lessons.

Callback implementation

Callback capabilities are often implemented in other languages by passing a function pointer to another function. The receiving function uses the passed function pointer to call another function when an interesting event happens. However, Java doesn't support function pointers. In this module, we will learn how to implement the callback mechanism using interfaces instead.

From the simple to the more complex

As usual, our approach will be to learn the material by reviewing programs that progress from very simple to more complex. As mentioned earlier, this topic consumes all of this module and two additional lessons on my website as well.

Meaningful scenarios

It is usually easier to understand abstract concepts if they are explained in terms of meaningful scenarios. In this case, our scenario will consist of a teacher and some students . In the beginning there will only be one student. Ultimately there will be many students and there will also be some animals in the classroom as well.

Registration

The students (and the animals) register themselves on the teachers roll book to be notified of interesting events. Initially the interesting event will simply be the teacher taking the roll. Ultimately the interesting event will be notification that it is either time for recess, or it is time for lunch.

Unicast and multicast scenarios

Initially, only one student receives notification of the one type of event. Ultimately, all of the students and all of the animals receive notification of both types of event (recess or lunch) but some of those who are notified choose to ignore the notification.

We will refer to the case where only one student is on the list as the unicast program. We will refer to the case where many students (and possibly animals as well) are on the list as the multicast program. This terminology was selected because it matches the terminology used in the JDK 1.1 documentation for the Delegation Event Model.

Without further discussion, let's look at some code.

Discussion and sample code

Unicast sample program

The purpose of this program is to develop a callback capability using Interfaces . This version of the program is designed to emphasize the structure of the process. Therefore an effort was made to avoid the requirement for any extra code so it doesn't do anything fancy.

A CallBack interface

This program defines a CallBack interface (interface named CallBack ) that can be used to establish a new type of object reference, and also to declare the interface to a method named callBack that will be contained in all objects of classes that implement the interface. This method will then be used to notify those objects whenever something interesting happens.

A Teacher class

The program defines a Teacher class that has the ability to

  • create and maintain a list of (only) one object of the interface type (multiple objects come later) , and
  • to notify that object that something interesting has happened by calling its callBack method.

(As mentioned earlier, the size of the list was constrained to only one object in order to emphasize callback structure and avoid getting bogged down in list processing. A subsequent version will implement list processing.)

A Student class

The program defines a class named Student that implements the CallBack interface. Objects of the Student class can be registered on the list maintained by an object of the Teacher class, and can be notified by the object of the Teacher class whenever something interesting happens. Notification takes the form of calling the callBack method on the object.

The method named callBack

The body of the callBack method can be designed to do anything, but in this case, to keep things simple, it just announces that it has been called.

The controlling class

Finally, the program defines a controlling class named Callback01 that ties all the pieces together and exercises them.

The program was originally tested using JDK 1.1.3 under Win95 and more recently tested using JDK 1.7 under Windows Vista.

The output from the program is shown in the complete program listing in a later section.

Interesting unicast code fragments

Listing 1 defines an interface named CallBack that creates a new type and declares a generic method named callBack that can be used to execute a callback on any object that is instantiated from a class that implements the interface.

Listing 1: Define the CallBack interface.

interface CallBack{
  public void callBack();
}//end interface CallBack

A class that can register and notify objects of type CallBack

Next we need a class whose objects can maintain a list of references to objects of type CallBack (objects whose class implements the CallBack interface) .

We refer to the process of putting an object on the list is registering the object.

This class also needs to have the ability to notify all the objects on that list when something interesting happens. We will name this class Teacher in keeping with the scenario described earlier.

As mentioned earlier, to keep things simple, and emphasize the callback structure without getting bogged down in list processing, we will begin with a limitation of one object for the length of the list.

The unicast class named Teacher

The unicast Teacher class consists of one instance variable of type CallBack (the interface type) and two instance methods.

One of the methods named register places an object on the list. The other method named callTheRoll calls the callBack method on the object that is on the list.

Note that the object on the list is guaranteed to have a method named callBack because it implements the CallBack interface. Otherwise, it couldn't get on the list in the first place. This is because the register method requires the incoming object's reference to be of type CallBack .

The Teacher class is defined in Listing 2 .

Listing 2: Define the Teacher class.

class Teacher{
  CallBack obj; //list of objects of type CallBack
  //-----------------------------------------------------//

  //Method to add objects to the list.
  void register(CallBack obj){
    this.obj = obj;
  }//end register()
  //-----------------------------------------------------//

  //Method to notify all objects on the list
  void callTheRoll(){
    obj.callBack();
  }//end callTheRoll()
  //-----------------------------------------------------//
}//end class Teacher

A class that implements the CallBack interface

Next, we need a class that implements the CallBack interface. Objects of this class can be registered on the list maintained by an object of the Teacher class, and will be notified whenever that object calls the callBack method on the registered objects on the list. In keeping with the scenario described earlier, we will name this class Student .

By claiming to implement the CallBack interface, this class is required to provide a concrete definition for the method named callBack that is declared in the interface. Otherwise, the program won't compile. In this case, that definition is rather simple. The callBack method simply announces that it has been called.

The callback mechanism

As we saw above, an object of the Teacher class will call the callBack method on all objects on its list when the interesting event occurs. It is important to note that the callback mechanism is to call this method.

The Student class is defined in Listing 3 .

Listing 3: Define the Student class.

class Student implements CallBack{
  String name;
  //-----------------------------------------------------//

  Student(String name){//constructor
    this.name = name;  //save the name to identify the obj
  }//end constructor
  //-----------------------------------------------------//

  public void callBack(){
    System.out.println(name + " here");
  }//end callBack()
}//end class Student

A controlling class named Callback01

Finally, we need a controlling class to tie all the pieces together and to exercise them. The main method in this class

  • instantiates an object of the Teacher class named missJones ,
  • instantiates an anonymous Student object named " Joe ",
  • registers the object on the list maintained by missJones , and
  • calls the callTheRoll method on missJones to cause the objects on the list to be notified (to cause their callBack methods to be called).

This is not too complicated once you break the process into its component parts.

The class named Callback01 is defined in Listing 4 .

Listing 4: A controlling class named Callback01.

class Callback01{
  public static void main(String[] args){
    //Instantiate Teacher object
    Teacher missJones = new Teacher();
    //Instantiate and register a Student object with the
    // Teacher object
    missJones.register(new Student("Joe"));
    //Cause the Teacher object to do a callBack on the
    // Student object.
    missJones.callTheRoll();
  }//end main()
}//end class Callback01

There you have it. This simple program contains the sum and substance of one approach to callbacks in Java.

It is critical to note that the objects registered on the list are of the interface type CallBack . This guarantees that there cannot be an object on the list that does not have an instance method named callBack .

Unicast Program Listing

A complete listing of the program is provided in Listing 5 so that you can view the code fragments in context.

Listing 5: Complete listing of program named Callback01.

/*File Callback01.java Copyright 1997, R.G.Baldwin
The purpose of this program is to develop a callback
capability using Interfaces.  This version of the
program is designed to emphasize the structure of
the process, and therefore an effort was made to
avoid the requirement for any extra code to do
anything fancy.

Tested using JDK 1.1.3 under Win95.

The output from the program is:

Joe here.
**********************************************************/
//First we define an interface that will create a new type
// and declare a generic method that can be used to
// callback any object that is of a class that implements
// the interface.
interface CallBack{
  public void callBack();
}//end interface CallBack
//=======================================================//

//Next we need a class whose objects can maintain a
// registered list of objects of type CallBack (whose
// class implements the CallBack interface) and can
// notify all the objects on that list when something
// interesting happens.

//To keep things simple, and emphasize the structure of
// what we are doing, we will begin with a limitation
// of one object on the length of the list.

class Teacher{
  CallBack obj; //list of objects of type CallBack
  //-----------------------------------------------------//

  //Method to add objects to the list.
  void register(CallBack obj){
    this.obj = obj;
  }//end register()
  //-----------------------------------------------------//

  //Method to notify all objects on the list that
  // something interesting has happened.
  void callTheRoll(){
    //Call the callBack() method on the object.  The
    // object is guaranteed to have such a method because
    // it is of a class that implements the CallBack
    // interface.
    obj.callBack();
  }//end callTheRoll()
  //-----------------------------------------------------//
}//end class Teacher
//=======================================================//

//Class that implements the CallBack interface.  Objects
// of this class can be registered on the list maintained
// by an object of the Teacher class, and will be notified
// whenever that object calls the callBack method on the
// registered objects on the list.
class Student implements CallBack{
  String name;
  //-----------------------------------------------------//

  Student(String name){//constructor
    this.name = name;  //save the name to identify the obj
  }//end constructor
  //-----------------------------------------------------//

  //An object of the Teacher class will call this method
  // as the callback mechanism to notify an object of this
  // class that something interesting has happened.
  public void callBack(){
    System.out.println(name + " here");
  }//end overridden callBack()
}//end class Student
//=======================================================//

//Controlling class that ties all the pieces together and
// exercises them.
class Callback01{
  public static void main(String[] args){
    //Instantiate Teacher object
    Teacher missJones = new Teacher();
    //Instantiate and register a Student object with the
    // Teacher object
    missJones.register(new Student("Joe"));
    //Cause the Teacher object to do a callBack on the
    // Student object.
    missJones.callTheRoll();
  }//end main()
}//end class Callback01
//=======================================================/

Multicast sample program

The multicast version of this program does not modify the basic callback mechanism developed in the previous program. It simply enhances that mechanism to make it possible to maintain a list of objects registered for callback and to notify all the objects on that list when an interesting event happens.

In case you started reading at this point, this is an enhanced version of the program named Callback01 . You should familiarize yourself with that program before trying to understand this program.

A list of registered objects

This program has the capability to create and maintain a list of objects that register for callback whereas the program named Callback01 could only remember a single object for callback.

Multiple classes implement CallBack interface

In addition, this program defines two different classes that implement the CallBack interface. Mixed objects of those two types are maintained on the list and notified at callback time. This is a subtle but very important point. It is not necessary that all the objects that are registered on a callback list be of the same class type, only that they all be of a class that implements the CallBack interface.

The CallBack interface

As before, this program defines a CallBack interface that establishes a new type of object, and also declares the interface to a method named callBack that is contained in all objects of classes that implement the interface. Because the callBack method is guaranteed to be contained in all of the objects on the list, it can be used to notify those objects whenever something interesting happens.

The Teacher class

The program defines a Teacher class that creates and maintains a list of objects of the CallBack interface type, and notifies those objects that something interesting has happened by calling the callBack method on each of the objects on the list.

The size of the list is limited only to the largest Vector object that can be accommodated by the system. (See the Java documentation or my online tutorials for information about the Vector class.)

The Student and Dog classes

The program defines a class named Student that implements the CallBack interface. The program also defines a class named Dog that implements the CallBack interface as well. (Back in the description of the scenario, I promised you that missJones was going to have to deal with animals in the classroom. I'm glad I don't have that problem.)

Registration and notification of Student and Dog objects

Objects of the Student and Dog classes can be registered on the list (of CallBack objects) maintained by an object of the Teacher class (because they both implement the CallBack interface) , and can be notified by the object of the Teacher class whenever something interesting happens.

Addition and removal from the list

Note that objects can be added to the list and then removed from the list. One object is first added and later removed for demonstration purposes.

The callback mechanism

As before, notification takes the form of calling the callBack method on each of the objects on the list.

Behavior of the callBack methods

The behavior of the callBack methods in the classes that implement the interface can be designed to do anything. In this case, to keep things simple, they just announce that they have been called. However, they make the announcement in slightly different ways.

Text display statements

This program contains display statements in the registration and notification methods for demonstration purposes only, and to allow us to track what is happening as the program runs.

The controlling class

Finally, the program defines a controlling class named Callback02 that ties all the pieces together and exercises them.

The program was originally tested using JDK 1.1.3 under Win95 and more recently tested using JDK 1.7 under Windows Vista.

The output from the program is shown following a discussion of the controlling class at the end of the next section.

Interesting multicast code fragments

Listing 6 defines an interface that creates a new type and declares a generic method that can be used to call back any object that is of a class that implements the interface. There is nothing new here.

Listing 6: Define the CallBack interface.

interface CallBack{
  public void callBack();
}//end interface CallBack

A class that can register and notify objects of type CallBack

Next we need a class whose objects can maintain a registered list of objects of type CallBack (objects whose class implements the CallBack interface) and can notify all the objects on that list when something interesting happens. As before, we name this class Teacher .

The Teacher class

The Teacher class has grown to the point that we will break it into parts and discuss them separately.

There is quite a bit here that is new, due simply to the requirement for list processing. There is nothing new about the basic callback mechanism.

An object of type Vector

We start out by replacing the single instance variable of type CallBack by a reference to an object of type Vector . We will maintain our list in an object of type Vector .

Recall that a Vector object can only work with references to objects of type Object , so this will entail some down casting later.

(Editor's note: Sometime around JDK 1.5, a concept known as generics was released into Java, which eliminated the restriction to objects of type Object mentioned in the previous paragraph. However, this code has not been updated to take advantage of that capability.)

The constructor for our new Teacher class, which is shown in Listing 7 , instantiates the Vector object.

Listing 7: Define the Teacher class.

class Teacher{
  Vector objList; //list of objects of type CallBack
  //----------------------------------------------//

  Teacher(){//constructor
    objList = new Vector();
  }//end constructor

The method named register

Next we need a method to add objects to the list. We will synchronize it to protect against the possibility of two or more objects on different threads trying to register at the same time.

Note that the references to the objects are received as type CallBack , which is the interface type, and stored as type Object , because the Vector class only accommodates references to objects of type Object . (See the earlier editor's note.) Again, this will lead to some down casting requirements later.

Listing 8: Define the method named register.

  synchronized void register(CallBack obj){
    this.objList.addElement(obj);
    System.out.println(obj + " added");
  }//end register()

The unRegister method

To be general, we also need a method to remove objects from the list. Removal of an object from the list is a little more complicated than adding an object to the list due to the possibility of having two or more identical objects on the list. (We could, and possibly should, guard against that possibility when constructing the list.)

Figure 1 contains a partial excerpt from the JDK 1.1.3 documentation, which describes the removeElement method of the Vector class that we are using to accomplish this (three different methods are available to remove objects from a Vector ).

Figure 1: Documentation for the removeElement method.
Documentation for the removeElement method.
public final synchronized boolean removeElement(Object obj)

This method removes the first occurrence of the argument from this 
vector. Indices beyond that point are adjusted appropriately

Parameters: obj - the component to be removed.

Returns: true if the argument was a component of this vector; 
         false otherwise.

Registered object removal code

Given that explanation, the code for removal of an object from the list is straightforward. The unRegister method is shown in Listing 9 .

Listing 9: The unRegister method.

  synchronized void unRegister(CallBack obj){
    if(this.objList.removeElement(obj))
      System.out.println(obj + " removed");
    else System.out.println(obj + " not in the list");
  }//end register()

The callTheRoll method

Now we need a method to notify all of the objects on the list that something interesting has happened. We will name this method callTheRoll to adhere to our classroom scenario.

Maintain the integrity of the callback list

One of the potential problems with this type of callback mechanism is that when the callback method is called on an object, that method might take a while to finish.

(As an aside, when writing callback methods, if they do anything significant in terms of time, the code in the method should probably spawn another thread to do the actual work and return as quickly as possible.)

This leads to the possibility that additional objects might attempt to register during that time interval. To protect against this, we make a copy of the state of the list object as it existed at the point in time that the decision was made to do the callbacks, and then perform the callbacks using that copy. That way, the original list is free to be updated as needed during this interval.

So, we start out by creating a clone of the list. We also synchronize this process to prevent the list from being modified while we are creating the clone.

Following this, we use a for loop to access all the objects on the list, and call the callBack method on those objects. (Actually, the list contains references to objects, and not the actual objects, so we are calling the method on the references.)

As promised earlier, we have to downcast from Object to CallBack to gain access to the callBack method in the objects.

Listing 10: Define the callTheRoll method.


  void callTheRoll(){
    Vector tempList;//save a temporary copy of list here

    synchronized(this){
      tempList = (Vector)objList.clone();
    }//end synchronized block

    for(int cnt = 0; cnt < tempList.size(); cnt++){
      ((CallBack)tempList.elementAt(cnt)).callBack();
    }//end for loop
  }//end callTheRoll()

End of the class named Teacher

That ends the discussion of the class named Teacher and brings us to the class named Student that implements the CallBack interface. This class hasn't changed. As indicated earlier, this version of the program also has a class named Dog that implements the interface. These two classes are essentially the same.

Define the class named Dog

Because of their similarity, and because they are essentially the same as in the previous program, I will simply show the class named Dog with no further discussion.

Listing 11: Define the class named Dog.

class Dog implements CallBack{
  String name; //store name here for later ID
  //-----------------------------------------------------//

  Dog(String name){//constructor
    this.name = name; //save the name to identify the obj
  }//end constructor
  //-----------------------------------------------------//

  //An object of the Teacher class will call this method
  // as the callback mechanism to notify an object of this
  // class that something interesting has happened.

  public void callBack(){//announce callBack

    System.out.println("Woof, Woof " + name);
  }//end overridden callBack()
}//end class Dog

The controlling class

That brings us to the controlling class named Callback02 that ties all the pieces together and exercises them. This class is shown in Listing 12 .

Listing 12: Define the class named Callback02.


class Callback02{
  public static void main(String[] args){
    //Instantiate Teacher object
    Teacher missJones = new Teacher();

 //Instantiate some Student objects
    Student tom = new Student("Tom");
    Student sue = new Student("Sue");
    Student peg = new Student("Peg");
    Student bob = new Student("Bob");
    Student joe = new Student("Joe");

    //Instantiate some Dog objects.
    Dog spot = new Dog("Spot");
    Dog fido = new Dog("Fido");
    Dog brownie = new Dog("Brownie");

    //Register some Student and Dog objects with the
    // Teacher object.
    System.out.println("Register Tom");
    missJones.register(tom);
    System.out.println("Register Spot");
    missJones.register(spot);
    System.out.println("Register Sue");
    missJones.register(sue);
    System.out.println("Register Fido");
    missJones.register(fido);
    System.out.println("Register Peg");
    missJones.register(peg);
    System.out.println("Register Bob");
    missJones.register(bob);
    System.out.println("Register Brownie");
    missJones.register(brownie);

    //Remove a Student object from the list.
    System.out.println("Remove Peg");
    missJones.unRegister(peg);

    //Try to remove an object that is not on the list.
    System.out.println("Try to remove Joe");
    missJones.unRegister(joe);

    System.out.println();//blank line

    //Cause the Teacher object to do a callBack on all
    // the objects on the list.
    missJones.callTheRoll();
  }//end main()
}//end class Callback02

Differences relative to Callback01

This program differs from the previous program primarily in terms of the volume of Student and Dog objects to be instantiated and registered on the Teacher object. There are also a lot of display statements to help us keep track of what is going on.

The ability to remove objects from the list is also illustrated.

Call the roll

Finally, the callback to the objects on the list is executed in Listing 12 by calling the callTheRoll method on the Teacher object named missJones . The output from running this program is shown later.

Mixed object types

A subtle, but extremely important point is illustrated here. Student and Dog are different classes. Objects of both of those classes are registered on the single object of the Teacher class. The Teacher object doesn't care that they are different, so long as they are all instantiated from classes that implement the CallBack interface. The register method will only accept object references of type CallBack .

Program output

The output from running this program is shown in Figure 2 . You can see the identification of each individual object as it is added to, or removed from the list.

Figure 2: Output from Callback02.
Output from Callback02.
Register Tom
Student@1cc73e added
Register Spot
Dog@1cc74e added
Register Sue
Student@1cc741 added
Register Fido
Dog@1cc751 added
Register Peg
Student@1cc744 added
Register Bob
Student@1cc747 added
Register Brownie
Dog@1cc754 added
Remove Peg
Student@1cc744 removed
Try to remove Joe
Student@1cc74a not in the list

Tom here
Woof, Woof Spot
Sue here
Woof, Woof Fido
Bob here
Woof, Woof Brownie

Note that the attempt to remove Joe from the list was not successful because he was never registered in the first place.

Finally, you see the output produced by calling callTheRoll which in turn calls the callBack method on each of the objects on the list.

Note that Peg didn't appear in the roll call because she was first added and then removed from the list before the roll call was taken.

The sum and substance

So there you have it, the sum and substance of multicast callbacks in Java. Obviously improvements could be made. You can see a couple of them in the remaining two tutorial lessons on callbacks that are published on my website.

Multicast Program Listing

A complete listing of the multicast program named Callback02 is provided in Listing 13 .

Listing 13: Complete listing of program named Callback02.

/*File Callback02.java Copyright 1997, R.G.Baldwin
The purpose of this program is to develop a callback
capability using Interfaces.

This is an enhanced version of the program named
Callback01. You should familiarize yourself with
the earlier program before getting into this program.

This version has the added capability to create and
maintain a list of objects that register for callback
whereas the program named Callback01 could only remember
a single object for callback.

Tested using JDK 1.1.3 under Win95.

The output from the program was:

Register Tom
Student@1cc73e added
Register Spot
Dog@1cc74e added
Register Sue
Student@1cc741 added
Register Fido
Dog@1cc751 added
Register Peg
Student@1cc744 added
Register Bob
Student@1cc747 added
Register Brownie
Dog@1cc754 added
Remove Peg
Student@1cc744 removed
Try to remove Joe
Student@1cc74a not in the list

Tom here
Woof, Woof Spot
Sue here
Woof, Woof Fido
Bob here
Woof, Woof Brownie

Note that Peg didn't appear in the callBack list because
she was first added to, and later removed from the list.
**********************************************************/
import java.util.*;

//First we define an interface that will create a new type
// and declare a generic method that can be used to
// callback any object that is of a class that implements
// the interface.
interface CallBack{
  public void callBack();
}//end interface CallBack
//=======================================================//

//Next we need a class whose objects can maintain a
// registered list of objects of type CallBack (whose
// class implements the CallBack interface) and can
// notify all the objects on that list when something
// interesting happens.

class Teacher{
  Vector objList; //list of objects of type CallBack
  //-----------------------------------------------------//

  Teacher(){//constructor
    //Instantiate a Vector object to contain the list
    // of registered objects.
    objList = new Vector();
  }//end constructor
  //-----------------------------------------------------//

  //Method to add objects to the list.  Synchronize to
  // protect against two or more objects on different
  // threads trying to register at the same time.  Note
  // that the objects are received as type CallBack which
  // is the interface type, and stored as type Object,
  // because the Vector class only accommodates objects of
  // type Object.
  synchronized void register(CallBack obj){
    this.objList.addElement(obj);
    System.out.println(obj + " added");
  }//end register()
  //-----------------------------------------------------//

  //Method to remove objects from the list.
  synchronized void unRegister(CallBack obj){
    if(this.objList.removeElement(obj))
      //true when successfully found and removed
      System.out.println(obj + " removed");
    else//false on failure to find and remove
      System.out.println(obj + " not in the list");
  }//end register()
  //-----------------------------------------------------//

  //Method to notify all objects on the list that
  // something interesting has happened.
  void callTheRoll(){
    Vector tempList;//save a temporary copy of list here

    //Make a copy of the list to avoid the possibility of
    // the list changing while objects are being notified.
    // Synchronize to protect against list changing while
    // making the copy.
    synchronized(this){
      tempList = (Vector)objList.clone();
    }//end synchronized block

    //Call the callBack() method on each object on
    // the list.  The object are guaranteed to have such
    // a method, even if they are of different types,
    // because they are all of a class that implements
    // the CallBack interface. If not, they could not
    // have been registered on the list in the first
    // place.  Note the requirement to downcast to
    // type CallBack.
    for(int cnt = 0; cnt < tempList.size(); cnt++){
      ((CallBack)tempList.elementAt(cnt)).callBack();
    }//end for loop
  }//end callTheRoll()
  //-----------------------------------------------------//
}//end class Teacher
//=======================================================//

//Class that implements the CallBack interface.  Objects
// of this class can be registered on the list maintained
// by an object of the Teacher class, and will be notified
// whenever that object calls the callBack method on the
// registered objects on the list.  This program will not
// compile if this class fails to implement the CallBack
// interface

class Student implements CallBack{
  String name; //store the object name here for later ID
  //-----------------------------------------------------//

  Student(String name){//constructor
    this.name = name;  //save the name to identify the obj
  }//end constructor
  //-----------------------------------------------------//

  //An object of the Teacher class will call this method
  // as the callback mechanism to notify an object of this
  // class that something interesting has happened.

  public void callBack(){//announce callBack
    System.out.println(name + " here");
  }//end overridden callBack()
}//end class Student
//=======================================================//

//Another Class that implements the CallBack interface.
// Objects of this class can also be registered on the list
// maintained by an object of the Teacher class, and will
// also be notified whenever that object calls the
// callBack() method on the registered objects on the
// list. This program will not compile if this class
// fails to implement the CallBack interface.

class Dog implements CallBack{
  String name; //store name here for later ID
  //-----------------------------------------------------//

  Dog(String name){//constructor
    this.name = name; //save the name to identify the obj
  }//end constructor
  //-----------------------------------------------------//

  //An object of the Teacher class will call this method
  // as the callback mechanism to notify an object of this
  // class that something interesting has happened.

  public void callBack(){//announce callBack
    System.out.println("Woof, Woof " + name);
  }//end overridden callBack()
}//end class Dog
//=======================================================//

//Controlling class that ties all the pieces together and
// exercises them.
class Callback02{
  public static void main(String[] args){
    //Instantiate Teacher object
    Teacher missJones = new Teacher();

    //Instantiate some Student objects
    Student tom = new Student("Tom");
    Student sue = new Student("Sue");
    Student peg = new Student("Peg");
    Student bob = new Student("Bob");
    Student joe = new Student("Joe");

    //Instantiate some Dog objects.
    Dog spot = new Dog("Spot");
    Dog fido = new Dog("Fido");
    Dog brownie = new Dog("Brownie");

    //Register some Student and Dog objects with the
    // Teacher object.
    System.out.println("Register Tom");
    missJones.register(tom);
    System.out.println("Register Spot");
    missJones.register(spot);
    System.out.println("Register Sue");
    missJones.register(sue);
    System.out.println("Register Fido");
    missJones.register(fido);
    System.out.println("Register Peg");
    missJones.register(peg);
    System.out.println("Register Bob");
    missJones.register(bob);
    System.out.println("Register Brownie");
    missJones.register(brownie);

    //Remove a Student object from the list.
    System.out.println("Remove Peg");
    missJones.unRegister(peg);

    //Try to remove an object that is not on the list.
    System.out.println("Try to remove Joe");
    missJones.unRegister(joe);

    System.out.println();//blank line

    //Cause the Teacher object to do a callBack on all
    // the objects on the list.
    missJones.callTheRoll();
  }//end main()
}//end class Callback02
//=======================================================//

Run the program

I encourage you to copy the code from Listing 5 and Listing 13 . Compile the code and execute it. Experiment with the code, making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

Summary

In this module, you learned the fundamentals of callbacks using interfaces in Java.

What's next?

In the next module, you will learn about something that goes by the name Delegation Event Model along with a few other names as well.

Miscellaneous

This section contains a variety of miscellaneous information.

Note:

Housekeeping material
  • Module name: Java OOP: Callbacks - I
  • File: Java0077.htm
  • Published: 1998
  • Revised: August 20, 2012

Note:

Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

-end-

Collection Navigation

Content actions

Download:

Collection as:

EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Module as:

PDF | More downloads ...

Add:

Collection to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks

Module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks