Why should we declare an interface inside a class?

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.

Leave a Comment