is that possible to create a inner class within an interface? If yes, why do we create like that? Anyways we are not going to create any interface objects?
Do they help in any Development process?
is that possible to create a inner class within an interface? If yes, why do we create like that? Anyways we are not going to create any interface objects?
Do they help in any Development process?
Yes, we can have classes inside interfaces. One example of usage could be
Here the code has two nested classes which are for encapsulating information about event objects which are later used in method definitions like getKeyEvents(). Having them inside the Input interface improves cohesion.
It certainly is possible, and one case where I've found it useful is when an interface has to throw custom exceptions. You the keep the exceptions with their associated interface, which I think is often neater than littering your source tree with heaps of trivial exception files.
A valid use, IMHO, is defining objects that are received or returned by the enclosing interface methods. Tipically data holding structures. In that way, if the object is only used for that interface, you have things in a more cohesive way.
By example:
But anyway... it's only a matter of taste.
For instance traits (smth like interface with implemented methods) in Groovy. They are compiled to an interface which contains inner class where all methods are implemented.
Yes it is possible to have static class definitions inside an interface, but maybe the most useful aspect of this feature is when using enum types (which are special kind of static classes). For example you can have something like this:
I'm needing one right now. I have an interface where it would be convenient to return a unique class from several of it's methods. This class only makes sense as a container for responses from methods of this interface.
Hence, it would be convenient to have a static nested class definition, which is associated only with this interface, since this interface should be the only place where this results container class is ever created.