<div dir="ltr" style="text-align: justify;" trbidi="on"><b>Anonymous classes</b> enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.</p>
<p> The<b> inner class</b> which is declared inside the body of a method is known as the<b> local inner classes</b>. The class declared inside the body of a method without naming it is known as <b>anonymous inner classes</b>.</p>
<p> The <b>anonymous inner classes</b> is very useful in some situation. For example consider a situation where you need to create the instance of an object without creating subclass of a class and also performing additional tasks such as method overloading.</p>
<div align='center' id="ads-id"></div>
<p>
 A class that have no name is known as <b>anonymous inner class</b>.<br />
Anonymous class can be created by:</p>
<ol>
<li>Class (may be abstract class also).</li>
<li>Interface</li>
</ol>
<p><b>Program of anonymous inner class by abstract class</b></p>
<pre class="highlight" name="code">abstract class Employee{ 
 abstract void work(); 
} 
 
class Manager{ 
 public static void main(String args[]){ 
 Employee emp = new Employee(){ 
 void work() 
 { 
 System.out.println("Manage the team");} 
 }; 
 emp.work(); 
 } 
} 
</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/inner1.png"/></div>
<p><b>After compiling we will get the following class files.</b></p>
<ul>
<li>Employee.class</li>
<li>Manager$1.class</li>
<li>Manager.class</li>
</ul>
<ol>
<li> A class is created but its name is decided by the compiler which extends the <i><b>Employee </b></i>class and provides the implementation of the <i><b>work()</b></i> method.</li>
<li>An object of <i><b>Anonymous </b></i>class is created that is referred by <b>emp </b>reference variable of <i><b>Employee </b></i>type. As you know well that Parent class reference variable can refer the object of Child class.</li>
</ol>
<p><b>The internal code generated by the compiler for annonymous inner class</b></p>
<pre class="highlight" name="code">import java.io.PrintStream; 
static class Manager$1 extends Employee 
{ 
 Manager$1(){ 
 
 } 
 void work() 
 { 
 System.out.println("Manage the team"); 
 } 
} 
</pre>
<p><b>Program of anonymous inner class by interface:</b></p>
<pre class="highlight" name="code">interface Employee{ 
 void work(); 
} 
 
class Manager{ 
 public static void main(String args[]){ 
 Employee emp = new Employee(){ 
 void work() 
 { 
 System.out.println("Manage the team");} 
 }; 
 emp.work(); 
 } 
} 
</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/inner1.png"/></div>
<p>
<b>The internal code generated by the compiler for annonymous inner class</b></p>
<pre class="highlight" name="code">import java.io.PrintStream; 
static class Manager$1 implements Employee 
{ 
 Manager$1(){ 
 
 } 
 void work() 
 { 
 System.out.println("Manage the team"); 
 } 
} 
</pre>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/member-inner-class/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/local-inner-classes-example-in-java/">Next</a> >;>;</b></div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/member-inner-class/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/local-inner-classes-example-in-java/">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: '489'});
});
</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…