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 ?
As mentioned in the section Nested Classes, nested classes enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code.
An interface, i.e., declared within another interface or class, is known as a nested interface. The nested interfaces are used to group related interfaces so that they can be easy to maintain. The nested interface must be referred to by the outer interface or class. It can't be accessed directly.
A nested class is a class that is declared in another class. The nested class is also a member variable of the enclosing class and has the same access rights as the other members. However, the member functions of the enclosing class have no special access to the members of a nested class.
In C#, a user is allowed to define a class within another class. Such types of classes are known as nested class. This feature enables the user to logically group classes that are only used in one place, thus this increases the use of encapsulation, and create more readable and maintainable code.
You can create an instance of ifaceClass
inside the 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 doesn't implement the interface, just create an instance like 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With