Interface inside Interface

Interface inside Interface

Note: It is advised to read the basics and types of inner classes before proceeding further.

A nested interface is just an interface but declared within another interface or even a class.

A top-level interface is that one which is not nested.

Nested interfaces are useful to group all the interfaces with some common functionality. It is to group the interfaces having some common functionality (purpose). Following two codes illustrates.

To construct a structure, three interfaces are giving the way of constructing pillars, ceiling and walls. Interfaces Pillars, Ceiling and Walls are having a common functionality.

One more practical approach can be seen as in the following code.

The usage here, you can observe, is no other application other than Structure can modify the number and span of pillars to construct the CommercialBuilding, even accidentally.

Let us go little bit deeper.

Following code also compiles and executes.

In the above code show() is not overridden as Outer1 is not implemented. It is done in the next program.

That is, the Test class can implement Outer1 and Inner1 separately or both together.

That is, following statement also executes.

Note: The implementing class can implement either the top-level interface alone or only nested interface or both.

Leave a Reply

Your email address will not be published. Required fields are marked *