Core JAVA

Annonymous inner class in java

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;" trbidi&equals;"on"><b>Anonymous classes<&sol;b> enable you to make your code more concise&period; They enable you to declare and instantiate a class at the same time&period; They are like local classes except that they do not have a name&period; Use them if you need to use a local class only once&period;<&sol;p>&NewLine;<p> The<b> inner class<&sol;b> which is declared inside the body of a method is known as the<b> local inner classes<&sol;b>&period; The class declared inside the body of a method without naming it is known as <b>anonymous inner classes<&sol;b>&period;<&sol;p>&NewLine;<p> The <b>anonymous inner classes<&sol;b> is very useful in some situation&period; 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&period;<&sol;p>&NewLine;<div align&equals;'center' id&equals;"ads-id"><&sol;div>&NewLine;<p>&NewLine; A class that have no name is known as <b>anonymous inner class<&sol;b>&period;<br &sol;>&NewLine;Anonymous class can be created by&colon;<&sol;p>&NewLine;<ol>&NewLine;<li>Class &lpar;may be abstract class also&rpar;&period;<&sol;li>&NewLine;<li>Interface<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><b>Program of anonymous inner class by abstract class<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">abstract class Employee&lbrace; &NewLine; abstract void work&lpar;&rpar;&semi; &NewLine;&rcub; &NewLine; &NewLine;class Manager&lbrace; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; Employee emp &equals; new Employee&lpar;&rpar;&lbrace; &NewLine; void work&lpar;&rpar; &NewLine; &lbrace; &NewLine; System&period;out&period;println&lpar;"Manage the team"&rpar;&semi;&rcub; &NewLine; &rcub;&semi; &NewLine; emp&period;work&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;04&sol;inner1&period;png"&sol;><&sol;div>&NewLine;<p><b>After compiling we will get the following class files&period;<&sol;b><&sol;p>&NewLine;<ul>&NewLine;<li>Employee&period;class<&sol;li>&NewLine;<li>Manager&dollar;1&period;class<&sol;li>&NewLine;<li>Manager&period;class<&sol;li>&NewLine;<&sol;ul>&NewLine;<ol>&NewLine;<li> A class is created but its name is decided by the compiler which extends the <i><b>Employee <&sol;b><&sol;i>class and provides the implementation of the <i><b>work&lpar;&rpar;<&sol;b><&sol;i> method&period;<&sol;li>&NewLine;<li>An object of <i><b>Anonymous <&sol;b><&sol;i>class is created that is referred by <b>emp <&sol;b>reference variable of <i><b>Employee <&sol;b><&sol;i>type&period; As you know well that Parent class reference variable can refer the object of Child class&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><b>The internal code generated by the compiler for annonymous inner class<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">import java&period;io&period;PrintStream&semi; &NewLine;static class Manager&dollar;1 extends Employee &NewLine;&lbrace; &NewLine; Manager&dollar;1&lpar;&rpar;&lbrace; &NewLine; &NewLine; &rcub; &NewLine; void work&lpar;&rpar; &NewLine; &lbrace; &NewLine; System&period;out&period;println&lpar;"Manage the team"&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>Program of anonymous inner class by interface&colon;<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">interface Employee&lbrace; &NewLine; void work&lpar;&rpar;&semi; &NewLine;&rcub; &NewLine; &NewLine;class Manager&lbrace; &NewLine; public static void main&lpar;String args&lbrack;&rsqb;&rpar;&lbrace; &NewLine; Employee emp &equals; new Employee&lpar;&rpar;&lbrace; &NewLine; void work&lpar;&rpar; &NewLine; &lbrace; &NewLine; System&period;out&period;println&lpar;"Manage the team"&rpar;&semi;&rcub; &NewLine; &rcub;&semi; &NewLine; emp&period;work&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&NewLine;<b>Output&colon;<&sol;b><&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img border&equals;"0" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;04&sol;inner1&period;png"&sol;><&sol;div>&NewLine;<p>&NewLine;<b>The internal code generated by the compiler for annonymous inner class<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight" name&equals;"code">import java&period;io&period;PrintStream&semi; &NewLine;static class Manager&dollar;1 implements Employee &NewLine;&lbrace; &NewLine; Manager&dollar;1&lpar;&rpar;&lbrace; &NewLine; &NewLine; &rcub; &NewLine; void work&lpar;&rpar; &NewLine; &lbrace; &NewLine; System&period;out&period;println&lpar;"Manage the team"&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><&sol;p>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;member-inner-class&sol;">Previous<&sol;a> &lt&semi;&lt&semi;&nbsp&semi;&nbsp&semi; &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;">Index <&sol;a>&vert;&vert; &nbsp&semi; &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;local-inner-classes-example-in-java&sol;">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;member-inner-class&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;local-inner-classes-example-in-java&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '489'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design PatternsĀ using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago