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?
What @Bachi mentions is similar to traits in Scala and are actually implemented using a nested class inside an interface. This can be simulated in Java. See also java traits or mixins pattern?
I found a use fir this type of construct.
You have access to all the constants grouped; name of the class acts as a namespace in this case.
Quote from the Java 7 spec:
It is NOT possible to declare non-static classes inside a Java interface, which makes sense to me.
Yes, you can create both a nested class or an inner class inside a Java interface (note that contrarily to popular belief there's no such thing as an "static inner class": this simply makes no sense, there's nothing "inner" and no "outter" class when a nested class is static, so it cannot be "static inner").
Anyway, the following compiles fine:
I've seen it used to put some kind of "contract checker" directly in the interface definition (well, in the class nested in the interface, that can have static methods, contrarily to the interface itself, which can't). Looking like this if I recall correctly.
Note that I'm not commenting on the usefulness of such a thing, I'm simply answering your question: it can be done and this is one kind of use I've seen made of it.
Now I won't comment on the usefulness of such a construct and from I've seen: I've seen it, but it's not a very common construct.
200KLOC codebase here where this happens exactly zero time (but then we've got a lot of other things that we consider bad practices that happen exactly zero time too that other people would find perfectly normal so...).
Maybe when you want more complex constructions like some different implementation behaviours, consider:
This is your interface and this will be the implementee:
May provide some static implementations, but won't that be confusing, I don't know.
You can also create "Helper" static classes for common functionality for the objects that implement this interface: