1

I have a callback interface.

public interface CallBack{
  void executeforConn();
}

abstract class CallbackImpl implements Callback {     
  void executeforConn(){
    executeStatements();
  }

  abstract void executeStatements();
}

In the caller the callback is called.

new CallbackImpl{
  @Override
  executeStatements(){
    //extend the method
  }
}

Callback callback = new CallbackImpl();
callback.executeforConn();

The caller calls the implementation of callback method. What I do not understand why is it called a callback method. I know by using interface you get the flexibility to register any class which implements CallBack with the Caller. It doesn't have to be just CallBackImpl.

learningUser
  • 428
  • 3
  • 8
  • 21

1 Answers1

0

What I do not understand why is it called a callback method.

It is called a "callback" because it allows the method that you passed the object to "call back" to your code.

For a more detailed explanation of the purpose and use of callbacks, read the Wikipedia article on callbacks.

Why callback method is used for polymorphism

A callback interface and (multiple) implementations could be viewed as a (rather uninteresting) example of polymorphism; e.g. one Callback API with multiple implementations that implement different behaviors.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216