47

Why should we declare an interface inside a class in Java?

For example:

public class GenericModelLinker implements IModelLinker {

  private static final Logger LOG =LoggerFactory.getLogger(GenericModelLinker.class);
  private String joinAsPropertyField;
  private boolean joinAsListEntry;
  private boolean clearList;
  private List<Link> joins;

  //instead of a scalar property
  private String uniqueProperty;

  public interface Link {

    Object getProperty(IAdaptable n);

    void setProperty(IAdaptable n, Object value);

  }
}
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Bhupati Patel
  • 1,400
  • 2
  • 13
  • 18

5 Answers5

40

When you want to gather some fields in an object in order to emphasize a concept, you could either create an external class, or an internal (called either nested (static ones) or inner).

If you want to emphasize the fact that this cooperative class makes strictly no sense (has no use) outside the original object use, you could make it nested/inner.

Thus, when dealing with some hierarchy, you can describe a "nested" interface, which will be implemented by the wrapping class's subclasses.

In the JDK, the most significant example would be Map.Entry inner interface, defined within Map interface and implemented by various ways by HashMap, LinkedHashMap etc...

And of course, Map.Entry needed to be declared as public in order to be accessible while iterating the map wherever the code is.

Dessa Simpson
  • 1,232
  • 1
  • 15
  • 30
Mik378
  • 21,881
  • 15
  • 82
  • 180
  • 1
    Ironically, the `Map.Entry` implementations actually are used by folks outside the context of a `Map`, for the functionality of a `Pair` class that Java lacks. Discussed [here](https://stackoverflow.com/a/16089354/642706). Nevertheless, a good Answer here. – Basil Bourque Sep 03 '19 at 00:09
14

If the interface definition is small and the interface will only be used by clients of the class it's defined in, it's a good way to organize the code. Otherwise, the interface should be defined in its own file.

Dave
  • 4,546
  • 2
  • 38
  • 59
5

This is inner interface. Java programming language allows defining inner classes and interfaces. This is typically useful if you want to limit visibility of this class or interface by scope of current outer class.

Some people use this mechanism for creating a kind of namespace. IMHO this is abuse of the language feature (in most cases).

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • But the interface is public, so has this goal even been achieved here? – Duncan Jones May 20 '13 at 11:40
  • Public but under the main Class, so not really public. The IMPLEMENTS won't work as they need the super to be an interface and not an interface under a class (I just faced this problem in a Spring app) – Chaibi Alaa Aug 25 '15 at 00:46
  • @ChaibiAlaa, You probably had problem with Spring because your inner class/initerface is not declared as public. – AlexR Aug 25 '15 at 07:22
  • You specific problem is out of scope of this question, so if you want to find solution to you problem you are welcome to ask your specific question. – AlexR Aug 25 '15 at 07:28
2

To encapsulate behavior in a generic and resuable way.

Apart from nice example of Map.Entry used by Map implementation classes another good example is implementation of Strategy Pattern, where a execution strategy is evaluated and applied internally.

class Test
{

..
interface Cipher {
 doAction();
}
class RAMPCipher implements Cipher{}
class DiskCipher implements Cipher{}
..
}
harsh
  • 7,502
  • 3
  • 31
  • 32
1

Inside your class you may need multiple implementations of an interface, which is only relevant to this particular class. In that case make it an inner interface, rather than a public / package-private one.
Only an interface inside a class can be declared private or protected. Sometimes, that makes sense, when the interface is only appropriate for use inside the outer class (or its subclasses).

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82