-1

Im using this code on this question :

   public void getString(final VolleyCallback callback) {
    StringRequest strReq = new StringRequest(Request.Method.GET, url, new     Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            result=response;
            callback.onSuccess(result);
        }
    }...
}}

public interface VolleyCallback{
    void onSuccess(String result);
}

my question is why this interface can be use to listening the response. as i know in java interface is some kind of contract and or some abstract class that has some method. Interface need to be implement before we actually use it but in those code above there are not any implement code....

but the new object based on that interface can be populate as listener. need some concise explanation why this concept happen.

thanks for any explanation.

Community
  • 1
  • 1
navotera
  • 321
  • 1
  • 15
  • [Interfaces and Inheritance](https://docs.oracle.com/javase/tutorial/java/IandI/index.html) – Jonny Henly Jun 29 '16 at 05:34
  • it between object and subobject not interface and object – navotera Jun 29 '16 at 05:34
  • 1
    @JonnyHenly u edit your answer sir.. :D.... it previously polymophism need more detail... if u would – navotera Jun 29 '16 at 05:40
  • if you take only a part of the code of a question / answer don't complain here.... please read carefully [the answer you posted](http://stackoverflow.com/a/28120209/3850595) where it states: *code inside activity:* `new VolleyCallback(){ @Override public void onSuccess(String result){` – Jordi Castilla Jun 29 '16 at 05:43
  • i add the link as i post the question.. i think those code is clear enough without copas the other code. – navotera Jun 29 '16 at 05:46

5 Answers5

2

The interface (Response.Listener<String>) is implemented as an anonymous class instance here :

....
new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        result=response;
        callback.onSuccess(result);
    }
}
....

This is equivalent to creating a class that implements Response.Listener<String> and then passing an instance of that class to the StringRequest constructor.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • where is new `Response.Listener()` { code. I never type it on my code... – navotera Jun 29 '16 at 05:39
  • 1
    @navotera What do you mean you didn't type it? I copied that code from your question. It's inside the invocation of the `StringRequest` constructor. – Eran Jun 29 '16 at 05:42
  • sorry sir...i didn't see it... vote 1 down vote accept The interface (Response.Listener) is implemented as an anonymous class instance here sorry sir what the connection between Response.listerner and interface VolleyCallback ? do u mean response listener actually impelement that interface ? – navotera Jun 29 '16 at 05:56
  • @navotera It wasn't clear from your question which interface you were asking about. Clearly, any place that calls the `getString` method would have to pass to it an instance of a class that implements the `VolleyCallback` interface. – Eran Jun 29 '16 at 06:00
1

This is called the Observer Pattern, or a variation of such, in which you are instantiating a new object that abides the contract of that interface and passing a reference of it to the caller, so it can make use of that object when it's done doing whatever it was doing. It is used because it's practical to define your behaviour in an anonymous class for such an small task.

If you have a lot of repeated behaviour in your callbacks, you could also make a concrete class that implements the callback interface and pass an instance of such to the caller.

fixmycode
  • 8,220
  • 2
  • 28
  • 43
  • class EventSource extends Observable.. but this is interface.. sorry im newbie is this same ? – navotera Jun 29 '16 at 05:47
  • It's the same concept, but this concept is used widely over a lot of classes in the Android SDK and Java in general, so don't be fooled just by its name, it's not what you need. – fixmycode Jun 29 '16 at 06:01
1

The idea is same as here

public class Button {
    private Callback callback;

    public Button(Callback callback) {
        this.callback = callback;
    }

    public void update() {
        // Check if clicked..
        callback.onClick(this);
    }

    public interface Callback {
        public void onClick(Button Button);
    }
}


Button b = new Button(new Callback() {
    @Override
    public void onClick(Button b) {
        System.out.println("Clicked");
    }
});

In your case StringRequest expects Object that implements method onResponse. Later this method will be callbacked, code body will be executed.

When you send an object you actually send a reference to it. A caller expects possibility to call some methods (it wants you to implement some interface).

Maxim G
  • 1,479
  • 1
  • 15
  • 23
  • it class not interface... if interface i believe we need implement before actually use the interface method it self – navotera Jun 29 '16 at 06:05
  • I use interface Callback, in your case it's [interface Response.Listener](http://afzaln.com/volley/com/android/volley/Response.Listener.html) – Maxim G Jun 29 '16 at 06:08
  • so when implementing the interface, in java code no always need `implement` keyword ? when it such interface would be used implement keyword when not thanks sir – navotera Jun 29 '16 at 06:14
  • Short answer yes. Check info about [Anonymous Classes](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html). And as @Jonny Henly pointed read about Interfaces and Inheritance. – Maxim G Jun 29 '16 at 06:22
0

When invoking the getString() function you will need to pass in an actual implementation of the interface.

Nguyen Tuan Anh
  • 1,036
  • 8
  • 14
0

It is also a view :-

Java's object-oriented model does not currently support method pointers, and thus seems to preclude using this comfortable mechanism. Java's support of interfaces provides a mechanism by which we can get the equivalent of callbacks.

tSantoSh
  • 1
  • 1
  • in the actuall use of class `public class CallMe` it would need implement the interface but on my code above there is no any implement keyword CMIIW – navotera Jun 29 '16 at 06:10