4

What is the difference between these two piece of code

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

is that inner interface of View class, if so we know that interface can not be instantiated

And here

soInterface.getAnswers().enqueue(new Callback<SOAnswersResponse>() {
      @Override
      public void onResponse(Call<SOAnswersResponse> call, retrofit2.Response<SOAnswersResponse> response) {

      }

      @Override
      public void onFailure(Call<SOAnswersResponse> call, Throwable t) {

      }
  });

its callback that it required in the enqueue method, what this syntax define, is it interface "CallBack<>" defined some where and we are calling it here as inline interface, but again interface can not be instantiated like callback syntax says "new CallBack(){}"

blackHawk
  • 6,047
  • 13
  • 57
  • 100
  • While interfaces (as other abstract classes) are not to be instantiated, anonymous inner classes that implement a given interface (or extends an abstract class) can be instantiated with the form `new Interface() { ... implementation of abstract methods... }` – Usagi Miyamoto Aug 15 '17 at 13:35
  • are abstract methods different from methods we define in interface – blackHawk Aug 15 '17 at 13:37
  • An interface in Java is a abstract class, with all methods being `public abstract` and all variable being `public static final`... Well at least before Java8 functional interfaces... – Usagi Miyamoto Aug 15 '17 at 13:38

5 Answers5

1

What is the difference between these two piece of code

Both are 2 different interface

View.OnClickListener - this interface helps you to listen to the View click action

Callback<SOAnswersResponse> - Try to get the response from the server

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
1

Both of them are interfaces:

  • CallBack is represents that the response(Callback) of Retrofit and when it is present you go into the onResponse or it failed to comeback (in the 10s time frame), be read(wrong parsing), or other reasons then onFailure will be executed.

  • While onClickListener will be listening to button click.

you can implement an interface of onClick listener in the Activity or Fragment and use button.setOnClickListener(this) same for Retrofit.

Mohammad Tabbara
  • 1,438
  • 1
  • 16
  • 31
0

Both View.OnClickListener and Callback are interfaces.

OnClickListener is nested inside View class. Retrofit Callback is not nested.

This is the Callback interface doc: https://square.github.io/retrofit/2.x/retrofit/retrofit2/Callback.html

interface can not be instantiated

We instantiate an anonymous class here. i.e. We implement the interface as an anonymous class and instantiate that anonymous class on the fly.

From the Java doc:

They [Anonymous class] enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.

Bob
  • 13,447
  • 7
  • 35
  • 45
  • Thank you :) could you give any link about implementation of interface as anonymous class, that things needs explanation a bit – blackHawk Aug 15 '17 at 13:48
0

Both classes in this case are interfaces with a callback. The anonymous class implements View.OnClickListener which has a callback on onClick. Same goes for the anonymous class implementing Callback<SOAnswersResponse>(). This time it has a typed parameter and two callbacks for onResponse and onFailure.

The statement that interfaces can not be instantiated is true, but you can create an anonymous class of it. See Can we create an instance of an interface in Java?

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
0

You're correct, it is not possible to instantiate an interface. An interface is the definition of a set of methods that must be implemented by a concrete class.

What your code is using, in both cases, is an anonymous inner class. The Java syntax allows you to specify that you would like a new instance of 'something' that implements the specified interface but you don't want to write all the code to do that. Your code only provides the implementations of the abstract methods defined by that interface.

If you look at the class files for your application you will see something like MyClass$1.class, which is a class file generated synthetically by the compiler. The compiler creates a class called MyClass$1 that implements the interface you've specified with the methods you've defined. The compiler will then make your code look something like this before compiling it:

btn.setOnClickListener(new MyClass$1());

The same applies to the second example but with a different interface, etc.

Speakjava
  • 3,177
  • 13
  • 15