Mohamad Sharifi
Mohamad Sharifi

Reputation: 21

What is the necessity of using interfaces in Java callbacks?

what is the difference between this two implementaions? Is it necessary to use an interface?

the first snipet uses interface the second does not

// interface implementation of callback
interface OnSearchListener {
    void onFound(String result);
}

class Main {

    public static void main(String[] args) {
        Search search = new Search();
        search.searchForSomething(new OnSearchListener() {
            @Override
            public void onFound(String result) {
                System.out.println(result);
            }
        });

    }
}

class Search {
    public void searchForSomething(OnSearchListener searchListener) {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(200);
                System.out.println("searching....");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        searchListener.onFound("found some result");

    }
}

****************

this is a callback without using an interface

class Search {
    public void searchForSomething() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(200);
                System.out.println("searching....");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Main main = new Main();
        main.onFound("found some result");
    }
}

class Main {

    public void onFound(String result) {
        System.out.println(result);
    }

    public static void main(String[] args) {
        Search search = new Search();
        search.searchForSomething();

    }
}

what is the importance of using the interface in callbacks? and what are the pros and cons of not using it?

Upvotes: 1

Views: 157

Answers (3)

Manuel
Manuel

Reputation: 15042

You are comparing apples and oranges.

  • The first case is a callback.
  • The second case is not a callback.

In the second case you create a new instance of Main with Main main = new Main(); so that is not a callback. In a callback you'd be calling back an existing instance of Main, in your example to deliver back search results.

You could make Main a singleton so there would only ever be one instance of Main, but that way of communicating between classes is out of scope of your question and has certain drawbacks, depending on your use case.

Upvotes: 0

Ralf Kleberhoff
Ralf Kleberhoff

Reputation: 7290

The term "callback" doesn't fit to the second implementation, as that's just a fixed method call.

"Callback" means that I supply code (in your case the OnSearchListener's onFound() method) to some function (searchForSomething()) asking it to call my code when some criteria are met. So, without any variability in the code supplied it isn't a callback.

Of course, if you have full control over both sides (Main and Search) and will only ever want one code snipped to get called, you don't need a callback, and that's your second implementation.

But if you want a real callback, you need an abstraction that defines the method signature, but not the implementation that is to be called, and that typically is an interface in Java, but (bad style!) could also be an abstract class or even a normal base class.

Upvotes: 2

Neerav Vadodaria
Neerav Vadodaria

Reputation: 317

Interfaces are always good to use if you have multiple implementations.. What if in the scenario you mentioned you different version of onFound?

Upvotes: 0

Related Questions