Search code examples
javainterfacenested-class

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


In which 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 struggling to make object of class ifaceClass.

EDIT :

I am able to make object like this

public class Test implements Iface {    

    public static void main(String[] args){
        ifaceClass  ifaceClassObj = new ifaceClass();
    }

    public void show() {    

    }
}

I noticed if Test has not implemented the Iface then I needed following import,

import com.jls.Iface.ifaceClass;

But it boiled down to same problem that why not use it as a just another class.

What the difference or value addition with this approach ?


Solution

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

    There is no such case which can only be fulfilled with inner class of interface. It is syntactically valid to have inner class in interface and for the class which implement interface can create instance of class and apart from that Interface.Class can also make that class accessible because it can not be private at all.

    I noticed if Test has not implemented the Iface then I needed following import import com.jls.Iface.ifaceClass;

    Not necessarily, if your interface is accessible your inner class will automatically become accessible.Here you are trying to access class directly without even importing interface in that case following statement need above import statement.

    ifaceClass  ifaceClassObj = new ifaceClass();
    

    But it boiled down to same problem that why not use it as a just another class. What the difference or value addition with this approach

    Exactly, creating another class can also provide you the same facility and I have never seen any use case in my day to day programming which can only be fulfilled with inner class of interface.It does not provide anything else than accessibility through the interface.

    I have used it once which I think quite a bad practice though. One day we need to implement one common method in different classes which are implementing interface say X and we wanted to add one extra method to be used by all this classes to add one kind of check on the Object which only check some parameter and return boolean even though that use case can be fulfilled in other way but to be specific that it is only intended for classes which are implementing this interface we have added class in interface so that we can provide that method to implementing classes.(NOTE : Nowadays default method can be used in this case instead of inner class)

    Here, it is wise to note that in huge projects it is quite impossible for anyone ( other than creator ) to note that any interface has inner class. So, until we implement that class or manually check the interface we can not came to know that interface has inner class.