100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Callback in OOP?

Learn what a callback is in OOP — interfaces, lambdas and inversion of control — with a Java example and common interview questions.

easyQ188 of 226 in Object Oriented Programming Est. time: 4 minsLast updated:
Open Code Lab
188 / 226

Expected Interview Answer

A callback in OOP is a piece of executable behavior, typically an object implementing an interface or a functional reference, that is passed into another method so that method can invoke it later once a particular condition or task completes.

Rather than a method knowing in advance exactly what should happen after it finishes, the caller supplies the “what to do next” as an argument, and the method invokes that callback at the appropriate moment, often after an asynchronous operation or a specific event. In classic Java, a callback is usually an object implementing a small interface with one method, such as a Runnable or a custom Callback interface; in languages with first-class functions or Java’s lambda support, it can be a lambda expression bound to a functional interface. This inverts control flow: the called method decides when to invoke the behavior, while the caller decides what that behavior is, which is why it is often called “don’t call us, we’ll call you,” or the Hollywood principle.

  • Lets a method’s behavior be customized without modifying the method itself
  • Enables asynchronous or event-based code to notify completion
  • Keeps calling code decoupled from the internal implementation details
  • Works naturally with interfaces, lambdas, and method references

AI Mentor Explanation

A twelfth man tells the physio, "call me the moment a player gets injured," and then goes about other duties, trusting the physio to reach out exactly when that condition happens rather than checking in constantly. The physio holds a reference to the twelfth man’s “respond to injury” behavior and invokes it at the right time. That is a callback: a piece of behavior handed over in advance, to be invoked later by someone else once a specific condition occurs.

Step-by-Step Explanation

  1. Step 1

    Define a callback contract

    Declare an interface (or functional interface) describing the method to invoke later.

  2. Step 2

    Accept the callback as a parameter

    The method that performs the work takes the callback as an argument.

  3. Step 3

    Perform the work

    The method does its task, possibly asynchronously or after some event.

  4. Step 4

    Invoke the callback

    Once the condition or task completes, the method calls the callback’s method, passing any relevant result.

What Interviewer Expects

  • A clear definition: behavior passed in to be invoked later
  • Mention of interfaces or functional interfaces/lambdas as the mechanism in Java
  • Understanding of synchronous vs asynchronous callback invocation
  • Awareness of the Hollywood principle / inversion of control

Common Mistakes

  • Confusing a callback with a regular return value
  • Not handling exceptions thrown inside the callback
  • Assuming all callbacks run asynchronously (many run synchronously)
  • Forgetting a callback reference can cause memory leaks if never released

Best Answer (HR Friendly)

A callback is a piece of code you hand to another method so it can run that code later, once some task or condition finishes. Instead of the caller checking repeatedly for completion, the method itself calls back into the supplied behavior exactly when it is ready.

Code Example

Callback interface invoked after a task completes
interface Callback<T> {
    void onComplete(T result);
}

class DownloadTask {
    void download(String url, Callback<String> callback) {
        // simulate finishing the work, then invoke the callback
        String result = "downloaded:" + url;
        callback.onComplete(result);
    }
}

DownloadTask task = new DownloadTask();
task.download("file.zip", result -> System.out.println("Done: " + result));

Follow-up Questions

  • What is the difference between a synchronous and an asynchronous callback?
  • How do lambdas relate to callback interfaces in Java?
  • What is “callback hell” and how is it typically avoided?
  • How does a callback differ from an event listener?

MCQ Practice

1. A callback is best described as?

A callback is executable behavior handed to another method so it can be invoked later, typically after a task or condition completes.

2. In modern Java, a simple callback is often implemented using?

Java lambdas provide a concise way to implement single-method functional interfaces used as callbacks.

3. What principle describes a method invoking a supplied callback rather than the caller polling for completion?

The Hollywood principle describes inversion of control where the framework or method calls back into supplied code.

Flash Cards

Callback in one line?Behavior passed into a method to be invoked later, typically after a task completes.

Typical Java mechanism?An interface with one method, or a lambda bound to a functional interface.

Related principle?The Hollywood principle — "don’t call us, we’ll call you."

Common pitfall?Not handling exceptions thrown from within the callback itself.

1 / 4

Continue Learning