What is the purpose of an interface within a Java class?

In the example code below, I have an inner class interface

, so I am using interface methods. But I don't see any effect with / without interface methods. Can anyone help me what is the purpose of adding including them?

public class Controller {

    FlowerCallBackReceiver mListener;

    @Override
    public void success(String s, Response response) {
        try { 
            mListener.onFetchProgress(flower);
        } catch (JSONException e) {
            mListener.onFetchFailed();
        }
        mListener.onFetchComplete();
    }

    @Override
    public void failure(RetrofitError error) {
        mListener.onFetchComplete();
    }

    public interface FlowerCallBackReceiver {
        void onFetchProgress(Flower flower);
        void onFetchComplete();
        void onFetchFailed();
    }
}

      

+3


source to share


2 answers


This nested interface declaration is just a simple organizational technique. It won't change the standard Java semantics interface

at all.

For example, developers use it to clean up the top level package namespace. We can say that this is a style.



Some quick examples of Java SE :

+2


source


There is no obvious reason to have this interface there, based on the code you showed.

Typically, an interface can be embedded within a class if the implementations of that class are to be used in the body of the rest of the class, for example if it Controller

had a method like:



void doSomething(FlowerCallBackReceiver callback) {
  // ...
}

      

But this interface is not used here, so it is not clear why it would be here.

0


source







All Articles