<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>A View From Above: Classic Java Method Dispatching Anger</title>
    <link>http://www.ghostganz.com/blog/articles/2007/05/18/classic-java-method-dispatching-anger</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Anders Bengtsson on programming and other things</description>
    <item>
      <title>Classic Java Method Dispatching Anger</title>
      <description>&lt;p&gt;&lt;strong&gt;On a common trap that a lot of us have walked into as our Java skills deepened:&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;You&amp;#8217;re solving some programming problem involving multiple types/classes when you realize that you can make great use of Java&amp;#8217;s method overloading. Just implement one method for each of the classes, and you&amp;#8217;re done! In this example, two subclasses of &lt;code&gt;Person&lt;/code&gt;:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;void doStuff(Worker worker) {
   ...
}&lt;/code&gt;&lt;/pre&gt;


	&lt;pre&gt;&lt;code&gt;void doStuff(Capitalist capitalist) {
   ...
}&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;Now if you just call &lt;code&gt;doStuff&lt;/code&gt; like this:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;Person person = ...
x.doStuff(person);&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;it should all work, right? It should call the correct method, depending on which class &lt;code&gt;person&lt;/code&gt; is. You probably feel a slight rush of pride when you look at how simple your solution is.&lt;/p&gt;


	&lt;p&gt;But of course it &lt;strong&gt;doesn&amp;#8217;t work&lt;/strong&gt;. Java&amp;#8217;s method calls must have their argument types known at compile-time&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;. You need to have &lt;code&gt;person&lt;/code&gt; cast to one of the subclasses, otherwise the compiler will complain that it doesn&amp;#8217;t know which of the two methods you mean. You mean &amp;#8220;either&amp;#8221;, but the compiler can&amp;#8217;t help you.&lt;/p&gt;


	&lt;p&gt;At this point there is often bitter disappointment and anger, usually directed at the compiler or language (&lt;em&gt;&amp;#8220;jävla skitspråk!&amp;#8221;&lt;/em&gt;). Once that subsides, you know you have learned something. Even programmers with good knowledge of Java seem to walk into this trap, which is what made me notice this phenomenon. Even though you know all the theory of how Java works, you can still get surprised by the practical implications.&lt;/p&gt;


	&lt;p&gt;For me, it was around 1999 at my first job. I can&amp;#8217;t remember the problem I was trying to solve, but I do remember the surprise and disappointment. I last witnessed it just a few weeks ago. When I mentioned the phenomenon to a friend, he recalled having gone through it himself, and also having witnessed it recently.&lt;/p&gt;


	&lt;p&gt;Does everyone go through this when they&amp;#8217;re learning Java?&lt;/p&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; This doesn&amp;#8217;t happen with dynamically typed languages, since they typically bind their methods at run-time. On the other hand most dynamically typed languages don&amp;#8217;t do method dispatching on &lt;em&gt;types&lt;/em&gt; at all. Some functional languages with fancier method dispatchers can do this stuff, maybe at the expense of slower method calls. Erlang&amp;#8217;s pattern matching of messages takes this feature to the extreme (though it&amp;#8217;s not exactly method calls). You could argue that Prolog is the very extreme of this, where &lt;em&gt;everything&lt;/em&gt; is fancy method dispatching.&lt;/p&gt;</description>
      <pubDate>Fri, 18 May 2007 19:20:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:d70739d4-7da0-4ac9-82bc-a78223d65c39</guid>
      <author>Anders</author>
      <link>http://www.ghostganz.com/blog/articles/2007/05/18/classic-java-method-dispatching-anger</link>
      <category>Java</category>
      <category>Programming</category>
      <category>languages</category>
      <category>learning</category>
    </item>
    <item>
      <title>"Classic Java Method Dispatching Anger" by Anders Grusell</title>
      <description>Haha, I have been there too...
And I always use double dispatch since then. 

I like the CLOS/Lisp approach to the problem where methods are not grouped by class at all but by generic functions, and therefore in CLOS methods are separate from data, which mean you can change or even add methods at runtime without changig the objects that you currently have in the system. 
So many workarounds you see in the form of design patterns that are alrady solved years ago in Lisp...
</description>
      <pubDate>Wed, 30 May 2007 05:00:55 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:f73963dc-93f4-4519-b7e4-b1e9314a1497</guid>
      <link>http://www.ghostganz.com/blog/articles/2007/05/18/classic-java-method-dispatching-anger#comment-68</link>
    </item>
    <item>
      <title>"Classic Java Method Dispatching Anger" by Johan Lind</title>
      <description>Remember that the double dispatch pattern i patented in the US to John Vlissides/IBM.. :)

&lt;a href="http://www.patentstorm.us/patents/6721807.html" rel="nofollow"&gt;http://www.patentstorm.us/patents/6721807.html&lt;/a&gt;</description>
      <pubDate>Sun, 27 May 2007 10:13:19 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:19533afc-8fd5-430d-8ebd-79c178fa95e0</guid>
      <link>http://www.ghostganz.com/blog/articles/2007/05/18/classic-java-method-dispatching-anger#comment-67</link>
    </item>
    <item>
      <title>"Classic Java Method Dispatching Anger" by Dan</title>
      <description>Hi Anders, one solution to this particular problem is to use double dispatch (&lt;a href="http://en.wikipedia.org/wiki/Double_dispatch" rel="nofollow"&gt;http://en.wikipedia.org/wiki/Double_dispatch&lt;/a&gt;).  This a neat trick that will give you the kind of polymorphic behaviour you are looking for without the need for any instanceof checks.</description>
      <pubDate>Sat, 19 May 2007 16:35:50 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:3684e0bd-3259-476f-b1ff-d63ec96d43f4</guid>
      <link>http://www.ghostganz.com/blog/articles/2007/05/18/classic-java-method-dispatching-anger#comment-65</link>
    </item>
  </channel>
</rss>
