{"id":292,"date":"2015-06-18T21:49:19","date_gmt":"2015-06-18T16:19:19","guid":{"rendered":"http:\/\/invinciblevirus.wordpress.com\/?p=292"},"modified":"2025-02-27T07:46:42","modified_gmt":"2025-02-27T07:46:42","slug":"inner-classes-in-java","status":"publish","type":"post","link":"https:\/\/saurabhjain.dev\/?p=292","title":{"rendered":"Inner Classes in Java"},"content":{"rendered":"<p>An Inner class is the one which is defined inside another class. This technique doesn&#8217;t seem appealing in the beginning but becomes interesting once we start diving deep to explore features like<\/p>\n<ol>\n<li>Connection of inner class with outer class<\/li>\n<li>Multiple inheritance<\/li>\n<li>Anonymous class<\/li>\n<\/ol>\n<p>Connection with outer class<\/p>\n<p>Inner class can access fields and methods of outer class as if they are its own.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-294\" src=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2014\/05\/inner-class.jpg\" alt=\"inner-class\" width=\"812\" height=\"614\" \/><\/p>\n<p>In above example, inner class &#8216;Engine&#8217; can invoke private fields &amp; methods of outer class &#8216;Car&#8217; like it would access its own members. This is possible because inner class keeps a reference of outer class object which is used to instantiate the inner class object. Like any other class keyword <em>new<\/em> is used to create object of inner class but there is a difference here &#8211; for inner class we need to specify object of outer class dot <em>new<\/em> keyword, e.g. c.new in this example.<\/p>\n<p>Multiple inheritance<\/p>\n<p>Inner class is second technique to achieve multiple inheritance in Java, first one being interfaces. There are real life problems where logical solution lies in a single class inheriting from multiple classes or abstract classes; this can be achieved by outer class extending one class and multiple inner classes extending different classes or multiple inner classes extending same base class differently. For example,<\/p>\n<p><a href=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2015\/06\/inner-class-multiple-inheritance1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-304\" src=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2015\/06\/inner-class-multiple-inheritance1.jpg?w=300\" alt=\"inner-class-multiple-inheritance\" width=\"478\" height=\"215\" \/><\/a><\/p>\n<p>Anonymous inner class<\/p>\n<p>It is a technique using which an object can be created of a class\u00a0definition by skipping class name. It can be useful in cases where we need only one object of a class.<\/p>\n<p><a href=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2015\/06\/inner-class-anonymous1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-305\" src=\"https:\/\/saurabhjain.dev\/wp-content\/uploads\/2015\/06\/inner-class-anonymous1.jpg?w=300\" alt=\"inner-class-anonymous\" width=\"484\" height=\"281\" \/><\/a><\/p>\n<p>In above example, getFuel() method of RacingCar returns an anonymous class object by implementing Fuel interface. Also, note the semi-colon to end the return statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An Inner class is the one which is defined inside another class. This technique doesn&#8217;t seem appealing in the beginning but becomes interesting once we start diving deep to explore features like Connection of inner class with outer class Multiple inheritance Anonymous class Connection with outer class Inner class can access fields and methods of &hellip; <a href=\"https:\/\/saurabhjain.dev\/?p=292\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Inner Classes in Java<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[9,31,32,38],"class_list":["post-292","post","type-post","status-publish","format-standard","hentry","category-java","tag-anonymous-class","tag-inner-class","tag-innerclasss","tag-multiple-inheritance"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=292"}],"version-history":[{"count":1,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/292\/revisions"}],"predecessor-version":[{"id":785,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=\/wp\/v2\/posts\/292\/revisions\/785"}],"wp:attachment":[{"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/saurabhjain.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}