Hi , I like to know can we define an interface within an interface. like
interface abc {
void show();
public interface xyz {
void read();
}
}
This was question asked in interview. Any real time use of this.
Hi , I like to know can we define an interface within an interface. like
interface abc {
void show();
public interface xyz {
void read();
}
}
This was question asked in interview. Any real time use of this.
Yes we can do it. The definition of nested interface in java is as follows :
A nested interface is any interface whose declaration occurs within the body of another class or interface. A top-level interface is an interface that is not a nested interface.
Refer this for more.
Further ...
One reason could be that the outer interface has a method that takes a callback implementation as argument. The nested interface is in that case the contract that the callback method must implement. I don't see a reason to declare that callback interface at top level.
public interface Processor {
void execute(NotificationListener listener);
interface NotificationListener {
void processingCompleted();
} }
Another good reading at sun site about this topic is here
In particular, notice that when you implement an interface, you are not required to implement any interfaces nested within.
You could have tested that for yourself and got a completely definitive, opinion-free, risk-free answer in about 30 seconds.
Waiting possibly forever for a possibly incorrect response on a forum is by comparison not a rational mode of enquiry.