{"id":290,"date":"2009-10-03T06:38:45","date_gmt":"2009-10-03T06:38:45","guid":{"rendered":"http:\/\/kubasek.com\/blog\/pragmatic_craftsman\/?p=290"},"modified":"2010-07-22T03:09:27","modified_gmt":"2010-07-22T03:09:27","slug":"java-inner-classes-part-4-multiple-inheritance-and-closures","status":"publish","type":"post","link":"https:\/\/pragmaticcraftsman.kubasek.com\/?p=290","title":{"rendered":"Java Inner Classes &#8211; Part 4 &#8211; Multiple Inheritance and Closures"},"content":{"rendered":"<p>Java gets a lot of blame for not allowing straight multiple inheritance and for not implementing closures. But according to Bruce Eckel, you can do a multiple inheritance in Java, you can do closures &#8211; sort of. You can accomplish that with inner classes!<\/p>\n<p>In this final Part 4 entry, I will concentrate on these two advanced topics.<\/p>\n<h2>Multiple inheritance<\/h2>\n<p>One way that you can do multiple inheritance is just by implementing two or more interfaces. Easy and already possible in Java.  But what if you did not have an interface, but rather abstract or concrete class. You can no longer just extend two of them &#8212; Java limitation. Inner classes provide different options.<\/p>\n<p>First, let&#8217;s take a look at two different ways you can implement multiple interfaces.<\/p>\n<pre class=\"brush:java\">\r\n\/\/ Two ways that a class can implement multiple interfaces.\r\n\/\/ Thinking in Java example\r\ninterface A {}\r\ninterface B {}\r\n\r\nclass X implements A, B {}\r\n\r\nclass Y implements A {\r\n  B makeB() {\r\n    \/\/ Anonymous inner class:\r\n    return new B() {};\r\n  }\r\n}\r\n\r\npublic class MultiInterfaces {\r\n  static void takesA(A a) { }\r\n  static void takesB(B b) { }\r\n\r\n  public static void main(String[] args) {\r\n    X x = new X();\r\n    Y y = new Y();\r\n    takesA(x);\r\n    takesA(y);\r\n    takesB(x);\r\n    takesB(y.makeB());\r\n  }\r\n} \/\/ \/:~\r\n<\/pre>\n<p>Note how class Y implements multiple interfaces. I admit that I have never used it like that. But it does implement two interfaces.<\/p>\n<p>What if you had an abstract or concrete class. You can&#8217;t extend two classes easily. Not without the use of inner classes! Here&#8217;s how inner classes allow you to do that.<\/p>\n<pre class=\"brush:java\">\r\n\/\/ With concrete or abstract classes, inner\u0105\r\n\/\/ classes are the only way to produce the effect\r\n\/\/ of \"multiple implementation inheritance.\"\r\n\/\/ Thinking in Java example\r\n\r\n\r\nclass D {}\r\n\r\nabstract class E {}\r\n\r\nclass Z extends D {\r\n  E makeE() {\r\n    return new E() {};\r\n  }\r\n}\r\n\r\npublic class MultiImplementation {\r\n  static void takesD(D d) {}\r\n  static void takesE(E e) {}\r\n\r\n  public static void main(String[] args) {\r\n\r\n  Z z = new Z();\r\n  takesD(z);\r\n  takesE(z.makeE());\r\n  }\r\n} \/\/ \/:~\r\n<\/pre>\n<p>Possible? Yes. Clean? Not really. But you can!<\/p>\n<h2>Inner Class Features<\/h2>\n<p>Eckel says that with inner classes you have these additional features:<\/p>\n<blockquote><p>1. The inner class can have multiple instances, each with its own state information that is independent of the information in the outer-class object.<\/p>\n<p>2. In a single outer class you can have several inner classes, each of which implements the same interface or inherits from the same class in a different way.<\/p>\n<p>3. The point of creation of the inner-class object is not tied to the creation of the outer-class object.<\/p>\n<p>4. There is no potentially confusing &#8220;is-a&#8221; relationship with the inner class; it&#8217;s a separate entity.<\/p><\/blockquote>\n<h2>Closures &amp; callbacks<\/h2>\n<p>What is a closure? &#8220;A closure is a callable object that retains information from the scope in which it was created,&#8221; says Eckel. If you&#8217;ve been reading this series, you know that an inner class maintains a link to the outer class &#8212; that&#8217;s in fact a closure.<\/p>\n<p>The following example illustrates a closure. It&#8217;s long, but it&#8217;s worth getting comfortable with. (Plus, it&#8217;s the final example in the series!)<\/p>\n<pre class=\"brush:java\">\r\n\/\/ Using inner classes for callbacks\r\ninterface Incrementable {\r\n  void increment();\r\n}\r\n\r\n\/\/ Very simple to just implement the interface:\r\nclass Callee1 implements Incrementable {\r\n  private int i = 0;\r\n  public void increment() {\r\n    i++;\r\n    System.out.println(i);\r\n  }\r\n}\r\n\r\nclass MyIncrement {\r\n  public void increment() {\r\n    System.out.println(\"Other operation\");\r\n  }\r\n  static void f(MyIncrement mi) {\r\n    mi.increment();\r\n  }\r\n}\r\n\r\n\/\/ If your class must implement increment() in\/\/ some other way, you must use an inner class:\r\nclass Callee2 extends MyIncrement {\r\n  private int i = 0;\r\n\r\n  public void increment() {\r\n    super.increment();\r\n    i++;\r\n    System.out.println(i);\r\n  }\r\n\r\nprivate class Closure implements Incrementable {\r\n  public void increment() {\r\n    \/\/ Specify outer-class method, otherwise\r\n    \/\/ you'd get an infinite recursion:\r\n    Callee2.this.increment();\r\n  }\r\n\r\n  Incrementable getCallbackReference() {\r\n    return new Closure();\r\n  }\r\n}\r\n\r\nclass Caller {\r\n  private Incrementable callbackReference;\r\n\r\n  Caller(Incrementable cbh) {\r\n     callbackReference = cbh;\r\n  }\r\n  void go() {\r\n    callbackReference.increment();\r\n  }\r\n}\r\n\r\npublic class Callbacks {\r\n  public static void main(String[] args) {\r\n    Callee1 c1 = new Callee1();\r\n    Callee2 c2 = new Callee2();\r\n\r\n    MyIncrement.f(c2);\r\n    Caller caller1 = new Caller(c1);\r\n    Caller caller2 = new Caller(c2.getCallbackReference());\r\n    caller1.go();\r\n    caller1.go();\r\n    caller2.go();\r\n\r\n    caller2.go();\r\n  }\r\n}\r\n \/* Output:Other operation11\r\n\r\n2\r\nOther operation\r\n2\r\nOther operation\r\n3\r\n*\/\/\/:~\r\n}\r\n<\/pre>\n<h2>Wrap-up<\/h2>\n<p>This has been a long series &#8212; a first for me. I have learned a great deal about inner classes. I hope you find these helpful as well.<\/p>\n<p>Inner classes have their uses. They can help you implement an elegant solution. They can help you accomplish things not easily doable using alternative ways. They can also complicate your code a great deal. They can make your code unreadable. Use it with care. \ud83d\ude42<\/p>\n<p><strong>Reference<\/strong><br \/>\n<?php book(\"Thinking in Java (4th)\", \"0131872486\") ?>, Bruce Eckel<br \/>\n<a href=\"http:\/\/pragmaticcraftsman.com\/2009\/09\/java_inner_classes_-_intro_1.php\">Java Inner Classes &#8211; Part 1 &#8211; Intro<\/a><br \/>\n<a href=\"http:\/\/pragmaticcraftsman.com\/2009\/09\/java_inner_classes_-_part_2.php\">Java Inner Classes &#8211; Part 2 &#8211; Anonymous<\/a><br \/>\n<a href=\"http:\/\/pragmaticcraftsman.com\/2009\/09\/java_inner_classes_-_part_3.php\">Java Inner Classes &#8211; Part 3 &#8211; Nested Classes<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java gets a lot of blame for not allowing straight multiple inheritance and for not implementing closures. But according to Bruce Eckel, you can do a multiple inheritance in Java, you can do closures &#8211; sort of. You can accomplish that with inner classes! In this final Part 4 entry, I will concentrate on these [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[17,26,56],"class_list":["post-290","post","type-post","status-publish","format-standard","hentry","category-java","tag-advanced","tag-innerclass","tag-java"],"_links":{"self":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/posts\/290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=290"}],"version-history":[{"count":2,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":479,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=\/wp\/v2\/posts\/290\/revisions\/479"}],"wp:attachment":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}