Where is it useful to have nested classes in an interface?

In what scenario can an interface have nested classes?

The following code is allowed and valid.

public interface Iface { void show(); class ifaceClass { int x; public ifaceClass() { System.out.println(x); } } } 

I am also trying to create an object of class ifaceClass.

EDIT:

I can make such an object

 public class Test implements Iface { public static void main(String[] args){ ifaceClass ifaceClassObj = new ifaceClass(); } public void show() { } } 

I noticed that if Test did not Iface , I needed the following import,

 import com.jls.Iface.ifaceClass; 

But it came down to the same problem, why not use it as just another class.

What is the difference or value addition with this approach?

+5
source share
2 answers

Where is it useful to have nested classes in an interface?

There is no such case that can only be done with an internal class of an interface. It is syntactically fair to have an inner class in the interface, and for a class that implements the interface, it can create an instance of the class, and in addition, Interface.Class can also make this class available, since it cannot be private at all.

I noticed that if Test did not implement Iface, then after import I needed import com.jls.Iface.ifaceClass;

Not necessarily, if your interface is available, your inner class will automatically become available. If you are trying to access the class directly without even importing an interface , in this case the following statement should be given above the import statement.

 ifaceClass ifaceClassObj = new ifaceClass(); 

But it came down to the same problem, why not use it as a fair other class. What is the difference or value addition with this approach

Similarly, creating another class can also provide you with the same object, and I have never seen any use in my day-to-day programming that can only be accomplished using an internal interface class. It provides nothing but accessibility through an interface.

I used it once, which I think is pretty bad practice. Once we need to implement one common method in different classes that implement the interface, say X , and we wanted to add one additional method that all these classes will use to add one kind of check on Object , which checks only some parameters and return boolean . although this use case can be done in a different way, but for the specific one, that it is intended only for classes that implement this interface, we added a class to the interface so that we can provide this method for implementation classes. (NOTE: Currently, the default method can be used in this case instead of the inner class)

It is worth noting here that in huge projects for anyone (except the creator) it is completely impossible to notice that any interface has an internal class. So, until we implement this class or manually check the interface, we cannot understand that the interface has an internal class.

+2
source

You can create an instance of ifaceClass inside a class that implements Iface :

 interface Iface { void show(); class ifaceClass { int x; public ifaceClass() { System.out.println(x); } } } public class Test implements Iface { public static void main(String args[]) { ifaceClass iface = new ifaceClass(); } @Override public void show() { // ... } } 

If the class does not implement the interface, simply create an instance of this:

 Iface.ifaceClass iface = new Iface.ifaceClass(); 

Why create a class inside an interface? Basically for the same reason you create a class inside another class to group related classes together.

+3
source

All Articles