How to implement Callback mechanism ( With Two parameterized Callbacks )

This is all about “how to implement” callback mechanism in Core Java with one interface and more parameterized callback methods .
Steps to implement it
  1. Interface Class: One interface has to be created. This interface reference will be passed from UI class to background class. This will have two callback methods defined which is having some parameters.
  2. Background Operation Class: One class for doing background operation. Which will accept interface reference as parameter with some parameters. One different conditions this class will notify UI class.
  3. Calling Class: One class which can call background operation and implement interface class. This class will pass interface reference to background operation class.

CallbackInterface .java

This interface will just play a role of callback. We have two callback with some parameters, which will be invoked at two different scenarios. This interface will be passed from UI class to background class. 


public interface CallbackInterface {
void notifyInbetween(int value);

void success(int value, String successNote);
} : For background operation

This callback class will accept reference of interface  “CallbackInterface”. Once some condition met then notify by first parameterized callback and once background work is being performed completely then inform(notify) it back to calling class with some parameters.


 * This callback class will accept reference of interface 
 * “CallbackInterface” and notify back to calling class.
public class CallbackWorkerDemo {

public CallbackWorkerDemo(CallbackInterface mCallbackInterface) {
// background operation.
int value = -1;
for (int i = 0; i < 10; i++) {
// if some condition met, then notify by one callback.
if (i > 3 && i< 6) {
value = i;
// Once all success then success callback.
mCallbackInterface.success(value, “Successfully completed”);

This class will responsible to do some background operation. This class will accept interface reference and once background operation is performed then notify back to main class with some parameters. This callback class will accept reference of interface “CallbackInterface” and notify back to calling class.


public class CallbackDemo {
static CallbackInterface mCallbackInterface = new CallbackInterface() {

// Here notification/callback will be received from background
// operation.

public void notifyInbetween(int value) {
// callback one
System.out.println(“notifyInbetween callback. value is ” + value);


public void success(int value, String successNote) {
// callback two
System.out.println(“success callback. Value is ” + value + ” —- ” + successNote);

public static void main(String args[]) {

CallbackWorkerDemo callbackWorkerThread = new CallbackWorkerDemo(


notifyInbetween callback. value is 4
notifyInbetween callback. value is 5
success callback. Value is 9 —- Successfully completed

If you like the post then please socialize it. Happy Sharing. Loudly Subscribing.

Leave a Reply

Your email address will not be published. Required fields are marked *