<div dir="ltr" style="text-align: justify;" trbidi="on">A nested interface is just a regular interface defined inside another class or interface. They are actually defined inside the body of the parent class, not only in the same file. The feature is useful for grouping related interfaces and for encapsulating interfaces in the classes where they are used.</p>
<p> <b>Nested interfaces facts:</b></p>
<ul>
<li>when declared inside another interface they can only be public</li>
<li>when declared inside classes they can accept any access modifiers</li>
<li>they are implicitly static</li>
<li>they can be implemented by any class (package level, nested or inner) as long as the access modifiers permit visibility</li>
<li>as with regular package level interfaces variables defined inside are considered to be constants (static public) regardless of whether the modifiers are specified</li>
<li>like the package level interfaces they can declare nested classes and interfaces</li>
</ul>
<p>While declaring nested interfaces in classes is useful and can help maintain a clean design, other constructs like classes nested in interfaces and interfaces nested in interfaces are of little benefit and seem outright weird and even dangerous.</p>
<div align="center" id="ads-id"></div>
<p><b>Syntax of nested interface which is declared within the interface</b></p>
<pre class="highlight" name="code">interface interface_name{ 
 ... 
 interface nested_interface_name{ 
 ... 
 } 
} 
</pre>
<p>
<b>Syntax of nested interface which is declared within the class</b></p>
<pre class="highlight" name="code">class class_name{ 
 ... 
 interface nested_interface_name{ 
 ... 
 } 
} 
</pre>
<p>
<b>Example of nested interface which is declared within the interface</b></p>
<pre class="highlight" name="code">interface Designable{ 
 void design(); 
 interface Message{ 
 void dispaly(); 
 } 
} 
 
class NestedDemo implements Designable.Message{ 
 public void dispaly(){ 
 System.out.println("Hello nested interface at Dinesh on Java"); 
 } 
 
 public static void main(String args[]){ 
 Designable.Message message = new NestedDemo();//upcasting here 
 message.dispaly(); 
 } 
} 
</pre>
<p>
<b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/staticinner1.png" /></div>
<p>
As you can see in the above example, we are accessing the <b><i>Message </i></b>interface by its outer interface <i><b>Designable </b></i>because it cannot be accessed directly. It is just like <b>Almira </b>inside the room, we cannot access the Almira directly because we must enter the room first.<b> In collection framework, sun micro-system has provided a nested interface Entry. Entry is the sub interface of Map i.e. accessed by Map.Entry</b>.</p>
<p> After compiling we get the following class files</p>
<ol>
<li><b>NestedDemo.class</b></li>
<li><b>Designable$Message.class</b></li>
<li><b>Designable.class</b></li>
</ol>
<p>
<b>Internal code generated by the java compiler for nested interface Message</b><br />
The java compiler internally creates public and static interface as displayed below:</p>
<pre class="highlight" name="code">public static interface Designable$Message 
 { 
 public abstract void dispaly(); 
 } 
</pre>
<p>
<b>Example of nested interface which is declared within the class</b><br />
Let&#8217;s see how can we define an interface inside the class and how can we access it.</p>
<pre class="highlight" name="code">class Design{ 
 interface Message{ 
 void dispaly(); 
 } 
} 
 
class NestedDemo implements Design.Message{ 
 public void dispaly(){ 
 System.out.println("Hello nested interface at Dinesh on Java"); 
 } 
 
 public static void main(String args[]){ 
 Design.Message message = new NestedDemo();//upcasting here 
 message.dispaly(); 
 } 
} 
</pre>
<p>
<b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2013/04/staticinner1.png" /></div>
<p>
After compiling we get the following class files</p>
<ol>
<li><b>NestedDemo.class</b></li>
<li><b>Design$Message.class</b></li>
<li><b>Design.class</b></li>
</ol>
<p>
<b>define a class inside the interface</b><br />
If we define a class inside the interface, java compiler creates a static nested class. Let&#8217;s see how can we define a class within the interface:</p>
<pre class="highlight" name="code">interface INT{ 
 class Demo{} 
} 
</pre>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/static-nested-classes-in-java/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/multithreading-in-java/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/static-nested-classes-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/sleeping-thread-using-sleep-method/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '486'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…