Java Reference
In-Depth Information
5.6. Nesting in Interfaces
You declare nested classes and interfaces in an interface for the same
reason that you declare nested classes and interfaces in a class: nested
classes and interfaces allow you to associate types that are strongly re-
lated to an interface inside that interface. For example, a class that was
used only to return multiple values from an interface's method could be
represented as a nested class in that interface:
interface Changeable {
class Record {
public Object changer;
public String changeDesc;
}
Record getLastChange();
// ...
}
The method getLastChange returns a Changeable.Record object that contains
the object that made the change and a string describing the change. This
class has meaning relative only to the Changeable interface, so making a
top-level class not only is unnecessary but would also separate it from
the context of its use. As a nested class it is tightly bound to its origin
and context, but it is a normal class in every other regard.
Another use for a nested class within an interface is to define a (partial or
complete) default implementation for that interface. A class that imple-
ments the interface could then choose to extend the default implement-
ation class or to forward method invocations to an instance of that class.
Any class or interface nested inside an interface is public and static.
 
Search WWH ::




Custom Search