Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of interface inside a Java class?

In the sample code below, I have an interface inside class so that I'm using the methods of interface. But i don't see any effect with/without interface methods. Can someone 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();
    }
}
like image 216
Anonymous Avatar asked Feb 28 '26 05:02

Anonymous


1 Answers

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

For instance, developers use it to clean up the top level package namespace. It's a matter a style, one may say.

Some quick Java SE examples:

  • interface Thread.UncaughtExceptionHandler
  • interface Map.Entry<K,V>
  • interface Policy.Parameters
  • interface DirectoryStream.Filter<T>
  • interface ServiceRegistry.Filter
  • etc
like image 196
Paulo Mattos Avatar answered Mar 02 '26 17:03

Paulo Mattos