7

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 {
    }
Community
  • 1
  • 1
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • Are you sure SomeClass implements "I2"? I would expect it to have to implement "I1.I2" to reference the inner interface. – Patricia Shanahan Jan 22 '14 at 08:51
  • @TheLostMind Instead of paraphrasing your code, can you show the actual names of the interfaces and methods? That might make the question (and answers) more relevant. – mrjink Jan 22 '14 at 08:52
  • @PatriciaShanahan - SomeClass implements Only I2... not I1.I2... I2 can be accessed directly (without I1..) – TheLostMind Jan 22 '14 at 08:53
  • @TheLostMind In this case, it makes sense, because `OnQueryResultSentListener`s will only ever be used by classes that implement `XClientSession`. It can be used to shown a relationship between the two, and you're not likely to ever need an `OnQueryResultSentListener` without an `XClientSession`. – mrjink Jan 22 '14 at 09:07
  • @mrjink - XAppAgentBase doesnt implement XClientSession.. it implements XClient.. – TheLostMind Jan 22 '14 at 09:08
  • `XAppAgentBase` cannot implement `OnQueryResultSentListener`. It can implement `XClientSession.OnQueryResultSentListener`. – Aniket Thakur Jan 22 '14 at 09:10
  • @TheLostMind Ok, so then don't? If the `OnQueryResultSentListener` is used in more places, it should maybe be an interface on its own. – mrjink Jan 22 '14 at 09:12
  • Check your codebase. If the code you have posted is correct then your codebase also has a `OnQueryResultSentListener` interface as a top level interface. – Aniket Thakur Jan 22 '14 at 09:12
  • 1
    Here is the example : [Map.Entry](http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html) – Not a bug Jan 23 '14 at 11:49

2 Answers2

1

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.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • Yes.. I know that static is redundant.. And I also know that its valid.. My point is - is it good?.. Can it cause any problems in the future?. Can it break the code/design in the future? – TheLostMind Jan 22 '14 at 09:02
0

In java it is absolutely valid to define an interface within another

It is a coding style to say Interface I2 exists to be used in I1 (or they are related with each other)

sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • 1
    My question is : "is keeping I2 in I1" an advantage in my specific case?. Is it a good design?. I know its valid.. – TheLostMind Jan 22 '14 at 08:47