{"id":12805,"date":"2015-05-15T10:00:43","date_gmt":"2015-05-15T10:00:43","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=12805"},"modified":"2020-09-01T07:07:54","modified_gmt":"2020-09-01T07:07:54","slug":"anonymous-inner-classes","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/anonymous-inner-classes\/","title":{"rendered":"Anonymous Inner Classes"},"content":{"rendered":"<p>Having dealt with inner classes in earlier posts this post deals with anonymous inner classes \u2013 an even more different type of code that causes more confusion for the amateur programmer.<\/p>\n<h4>What are anonymous inner classes?<\/h4>\n<p>\u2018Anonymous inner classes\u2019 or \u2018Anonymous classes\u2019 as the name suggests is creating an inner class with no name. Here is a simple program that illustrates the working of the anonymous class.<\/p>\n<p>Program 1:<\/p>\n<pre>\/\/Program to illustrate anonymous inner class\n<strong>package<\/strong> whizlabs;\n<strong> class<\/strong> anon_1 {\n\u00a0\u00a0 \u00a0 <strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String args[]){\u00a0\u00a0\u00a0\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pgm p1=<strong>new<\/strong> pgm();\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0pgm p=<strong>new<\/strong> pgm(){ <strong>\/\/ start of anonymous inner class\n<\/strong>\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0String s=\"whizlabs\";\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0<strong>public<\/strong> <strong>void<\/strong> simple(){\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0System.<strong><em>out<\/em><\/strong>.println(s + \" world\");\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 }; <strong>\/\/ end of the anonymous inner class \n<\/strong>\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 p.simple();\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 p1.simple();\n\u00a0\u00a0 \u00a0 }\n\u00a0\u00a0 \u00a0 }\n<strong>class<\/strong> pgm{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>public<\/strong> <strong>void<\/strong> simple(){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(\"hello\");\n\u00a0\u00a0\u00a0\u00a0 }\n}<\/pre>\n<p>Output:<br \/>\nwhizlabs world<br \/>\nhello<\/p>\n<p>These are some key points:<\/p>\n<ol>\n<li>The above example shows two classes \u2018anon_1\u2019 and \u2018pgm\u2019 .<\/li>\n<li>The class \u2018pgm\u2019 has a method \u2018simple\u2019 which just prints a message \u2018hello\u2019.<\/li>\n<\/ol>\n<p>The main idea behind using anonymous inner classes is to override a method without the hassle of having to create a whole new class.<\/p>\n<p>This is the anonymous class code segment:<\/p>\n<pre>\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0pgm p=<strong>new<\/strong> pgm(){ <strong><span style=\"color: #ff0000\">\/\/ start of anonymous inner class<\/span>\n<\/strong>\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0String s=\"whizlabs\";\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0<strong>public<\/strong> <strong>void<\/strong> simple(){\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0System.<strong><em>out<\/em><\/strong>.println(s + \" world\");\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 }; <span style=\"color: #ff0000\"><strong>\/\/ end of the anonymous inner class<\/strong><\/span><\/pre>\n<p>Notice that the place where a semi-colon is normally present, an open paranthesis is present<\/p>\n<pre>pgm p=<strong>new<\/strong> pgm()<span style=\"color: #ff0000\"><strong>{\n<\/strong><\/span><\/pre>\n<p>This signals the start of the anonymous class.<\/p>\n<p>This class is now actually a sub-class of the \u2018pgm\u2019 class but with <strong><em>no name!<\/em><\/strong> The instance variable \u2018p\u2019 is created without the anonymous inner class even having a name. This anonymous class effectively allows us to override the \u2018simple\u2019 method in our case.<\/p>\n<p>The anonymous inner class is finally closed by a \u2018semi-colon\u2019 at the end of a brace.<\/p>\n<p>}; <span style=\"color: #ff0000\"><strong>\/\/ end of the anonymous inner class<\/strong><\/span><\/p>\n<p>p.simple() when invoked calls the overridden method \u2018simple\u2019 within the anonymous class and the appropriate output is printed.\u00a0 p1.simple() on the other hand invokes the super class\u2019s method and the appropriate output is printed out.<\/p>\n<p>Having seen this, let us see what happens when non-overriden method is introduced in the anonymous class.<\/p>\n<p>Program 2: Non-overriden method in anonymous class:<\/p>\n<p>\/\/Program to illustrate anonymous inner class<\/p>\n<pre><strong>package<\/strong> whizlabs;\n\u00a0<strong>class<\/strong> anon_1 {\n\u00a0\u00a0 \u00a0 <strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String args[]){\u00a0\u00a0\u00a0\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pgm p1=<strong>new<\/strong> pgm();\n\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0pgm p=<strong>new<\/strong> pgm(){ \/\/ start of anonymous inner class\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0String s=\"whizlabs\";\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0<strong>public<\/strong> <strong>void<\/strong> simple(){\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0System.<strong><em>out<\/em><\/strong>.println(s + \" world\");\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ Non-overridden method in anonymous class\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0<strong>public<\/strong> <strong>void<\/strong> another(){\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0System.<strong><em>out<\/em><\/strong>.println(\"another method\");\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 }; \n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 p.simple();\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>p.another();<\/strong>\/\/ trying to invoke the non-overridden method\n\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0 p1.simple(); \n\u00a0\u00a0 \u00a0 }\n\u00a0\u00a0 \u00a0 }\n<strong>class<\/strong> pgm{ \n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>public<\/strong> <strong>void<\/strong> simple(){\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(\"hello\");\n\u00a0\u00a0\u00a0\u00a0 }\n}<\/pre>\n<p>Explanation:<br \/>\nThis program shows a non-overridden method \u2018another\u2019 in the anonymous class.<\/p>\n<pre>\u00a0\u00a0\u00a0\u00a0 \u00a0 <strong>public<\/strong> <strong>void<\/strong> another(){\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0System.<strong><em>out<\/em><\/strong>.println(\"another method\");\n\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}<\/pre>\n<p>Just declaring it will not cause problems \u2013 however invoking it, the following way:<\/p>\n<p>p.another();<\/p>\n<p>will cause compiler error.<br \/>\nThis is the error that will be generated:<\/p>\n<p><span style=\"color: #ff0000\">Exception in thread &#8220;main&#8221; java.lang.Error: Unresolved compilation problem:<\/span><br \/>\n<span style=\"color: #ff0000\">\u00a0 \u00a0 \u00a0 \u00a0The method another() is undefined for the type pgm<\/span><br \/>\n<span style=\"color: #ff0000\">\u00a0 \u00a0 \u00a0 \u00a0at whizlabs.anon_1.main(<span style=\"text-decoration: underline\">anon_1.java:18<\/span>)\u00a0 (Kathy Sierra)<\/span><\/p>\n<p>Why is this error generated? It is because of polymorphism. Note that even though the variable \u2018p\u2019 refers to the anonymous sub-class , it is of the super-class \u2018pgm\u2019 type. Since the class \u2018pgm\u2019 does not have \u2018another\u2019 method, an error is thrown.<\/p>\n<p>In short, even though other methods can be declared within an anonymous inner class, they cannot be invoked. For how will we invoke a method that is within a class (that which has no name?)<\/p>\n<h5>Bibliography<\/h5>\n<h5>In B. B. Kathy Sierra, <em>Sun Certified Java Programmer for Java 6 Study Guide.<\/em><\/h5>\n","protected":false},"excerpt":{"rendered":"<p>Having dealt with inner classes in earlier posts this post deals with anonymous inner classes \u2013 an even more different type of code that causes more confusion for the amateur programmer. What are anonymous inner classes? \u2018Anonymous inner classes\u2019 or \u2018Anonymous classes\u2019 as the name suggests is creating an inner class with no name. Here is a simple program that illustrates the working of the anonymous class. Program 1: \/\/Program to illustrate anonymous inner class package whizlabs; class anon_1 { \u00a0\u00a0 \u00a0 public static void main(String args[]){\u00a0\u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 pgm p1=new pgm(); \u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0pgm p=new pgm(){ \/\/ start of [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[933,967],"class_list":["post-12805","post","type-post","status-publish","format-standard","hentry","category-java","tag-inner-class","tag-java-3"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":0,"uagb_excerpt":"Having dealt with inner classes in earlier posts this post deals with anonymous inner classes \u2013 an even more different type of code that causes more confusion for the amateur programmer. What are anonymous inner classes? \u2018Anonymous inner classes\u2019 or \u2018Anonymous classes\u2019 as the name suggests is creating an inner class with no name. Here&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=12805"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12805\/revisions"}],"predecessor-version":[{"id":75991,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/12805\/revisions\/75991"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=12805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=12805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=12805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}