{"id":1220,"date":"2022-02-25T17:04:10","date_gmt":"2022-02-25T17:04:10","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1220"},"modified":"2022-02-25T17:04:10","modified_gmt":"2022-02-25T17:04:10","slug":"java-anonymous-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/25\/java-anonymous-class\/","title":{"rendered":"Java Anonymous Class"},"content":{"rendered":"\n<p>In this tutorial, you will learn about anonymous classes in Java with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">In Java, a class can contain another class known as nested class. It&#8217;s possible to create a nested class without giving any name.<\/p>\n\n\n\n<p>A nested class that doesn&#8217;t have any name is known as an anonymous class.<\/p>\n\n\n\n<p>An anonymous class must be defined inside another class. Hence, it is also known as an anonymous inner class. Its syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class outerClass {\n\n    \/\/ defining anonymous class\n    object1 = new Type(parameterList) {\n         \/\/ body of the anonymous class\n    };\n}\n<\/code><\/pre>\n\n\n\n<p>Anonymous classes usually extend subclasses or implement interfaces.<\/p>\n\n\n\n<p>Here,&nbsp;<strong>Type<\/strong>&nbsp;can be<\/p>\n\n\n\n<ol><li>a superclass that an anonymous class extends<\/li><li>an interface that an anonymous class implements<\/li><\/ol>\n\n\n\n<p>The above code creates an object,&nbsp;<code>object1<\/code>, of an anonymous class at runtime.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;Anonymous classes are defined inside an expression. So, the semicolon is used at the end of anonymous classes to indicate the end of the expression.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-1\">Example 1: Anonymous Class Extending a Class<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Polygon {\n   public void display() {\n      System.out.println(\"Inside the Polygon class\");\n   }\n}\n\nclass AnonymousDemo {\n   public void createClass() {\n\n      \/\/ creation of anonymous class extending class Polygon\n      Polygon p1 = new Polygon() {\n         public void display() {\n            System.out.println(\"Inside an anonymous class.\");\n         }\n      };\n      p1.display();\n   }\n}\n\nclass Main {\n   public static void main(String&#91;] args) {\n       AnonymousDemo an = new AnonymousDemo();\n       an.createClass();\n   }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Inside an anonymous class.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a class&nbsp;<var>Polygon<\/var>. It has a single method&nbsp;<code>display()<\/code>.<\/p>\n\n\n\n<p>We then created an anonymous class that extends the class&nbsp;<var>Polygon<\/var>&nbsp;and overrides the&nbsp;<code>display()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>When we run the program, an object&nbsp;<var>p1<\/var>&nbsp;of the anonymous class is created. The object then calls the&nbsp;<code>display()<\/code>&nbsp;method of the anonymous class.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example-2\">Example 2: Anonymous Class Implementing an Interface<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Polygon {\n   public void display();\n}\n\nclass AnonymousDemo {\n   public void createClass() {\n\n      \/\/ anonymous class implementing interface\n      Polygon p1 = new Polygon() {\n         public void display() {\n            System.out.println(\"Inside an anonymous class.\");\n         }\n      };\n      p1.display();\n   }\n}\n\nclass Main {\n   public static void main(String&#91;] args) {\n      AnonymousDemo an = new AnonymousDemo();\n      an.createClass();\n   }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Inside an anonymous class.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an anonymous class that implements the&nbsp;<code>Polygon<\/code>&nbsp;interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"advantages\">Advantages of Anonymous Classes<\/h2>\n\n\n\n<p>In anonymous classes, objects are created whenever they are required. That is, objects are created to perform some specific tasks. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Object = new Example() {\n   public void display() {\n      System.out.println(\"Anonymous class overrides the method display().\");\n   }\n};\n<\/code><\/pre>\n\n\n\n<p>Here, an object of the anonymous class is created dynamically when we need to override the&nbsp;<code>display()<\/code>&nbsp;method.<\/p>\n\n\n\n<p>Anonymous classes also help us to make our code concise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn about anonymous classes in Java with the help of examples. In Java, a class can contain another class known as nested class. It&#8217;s possible to create a nested class without giving any name. A nested class that doesn&#8217;t have any name is known as an anonymous class. An anonymous [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[556],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1220"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1220"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1220\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}