Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of nested interfaces in this code

Tags:

java

I have gone through the following link Why would a static nested interface be used in Java?.

In my code base I have:

public interface I1{

   public static interface I2 {
        public void doSomething();
   }

    //some other methods

    public void myMethod(I2 myObject); 
}

And in some other class in a different package :

public abstract class SomeClass implements I2{
   //mandatory method...
}

Now, my question is - "Is it really a good design to put I2 in I1"?

EDIT :

public interface XClientSession {
static public interface OnQueryResultSentListener {

        public void onQueryResultSent(XQueryResult result);
    }
 public void setOnQueryResultSentListener(OnQueryResultSentListener listener);

}

/ And in a different file I have...

 public abstract class XAppAgentBase extends IntentService 
    implements XClient, OnQueryResultSentListener {
    }
like image 936
TheLostMind Avatar asked Jan 22 '14 08:01

TheLostMind


1 Answers

There is no need to use static keyword for inner interface as interface declared inside an interface is by default static similar to saying variables defined in interfaces are be default public and static.

Is it a good design? - Depends on the design for which it is created for. Your code constrains accessibility of I2 interface to only those part of codes that have accessible to I1 Interface.

like image 182
Aniket Thakur Avatar answered Oct 19 '22 09:10

Aniket Thakur