Q1. Can I have an interface inside a class in java?
Q2. Can I have an class inside an interface?
If yes, then in which situations should such classes/interfa
I have faced providing common complex operations for all classes implementing an interface, that use obviously the operations of the interface.
Until Java 8 is not out...
See http://datumedge.blogspot.hu/2012/06/java-8-lambdas.html (Default Methods)
A workaround for this is:
public interface I
{
public Class U{
public static void complexFunction(I i, String s){
i.f();
i.g(s)
}
}
}
Then you can easily call common functionality (after importing I.U)
U.complexFunction(i,"my params...");
May further refined, with some more typical coding:
public interface I
{
public Class U{
I me;
U(I me){
this.me = me;
}
public void complexFunction(String s){
me.f();
me.g(s)
}
}
U getUtilities();
}
class implementationOfI implements I{
U u=new U(this);
U getUtilities(){ return u; }
}
calling then
I i = new implementationOfI();
i.getUtilities().complexFunction(s);
A further spicy tricks
The reason for this is to put the operations into a single module instead of having utilities modules hanging around, allowing extendable the worst, duplicated implementation.