Hacker News new | comments | show | ask | jobs | submit login
Multiple Inheritance and Traits in Java (insightfullogic.com)
20 points by rlmw on Sept 16, 2011 | hide | past | web | favorite | 9 comments



If you are using mulitple inheritance to reuse code, you should think twice (even with regular inheritance). Unless there is a meaningful is-a relationship, this is best accomplished via composition.

If you are in the Java world, this is where it pays to switch some classes to Groovy and use its @Delegate annotation. It's a compile-time AST transformation, such that straight-up Java code can take advantage, and it can inject the delegated interfaces into its parent class (so now the class can show up as implementing an interface for the dependency injection framework).

This can be introduced on an as-needed basis (make some files Groovy and the Java code can call them) and Groovy is essentially 95% compatible with identical Java code, so you can usually rename .java to .groovy and be good to go.


> If you are in the Java world, this is where it pays to switch some classes to Groovy and use its @Delegate annotation. It's a compile-time AST transformation

One of the authors of the @Delegate annotation (http://groovy.codehaus.org/gapi/groovy/lang/Delegate.html) went on to create Groovy++, a statically-typed booster for the dynamic Groovy Language, also working using an AST transformation. Just put @Typed in front of the your package, class, or method, and you'll see speed increases of 10 to 50 times over bare Groovy ( http://code.google.com/p/groovypptest/wiki/Performance ).

Unfortunately, the developers of Groovy aren't bundling it with their distro of Groovy. You'll need to go direct to http://code.google.com/p/groovypptest/downloads/list , or pick it up from http://groovy.codeplex.com .


"If you are using mulitple inheritance to reuse code, you should think twice (even with regular inheritance). Unless there is a meaningful is-a relationship, this is best accomplished via composition."

Multiple inheritance is not just for specialization. See http://en.wikipedia.org/wiki/Mixin


I wish Java had no inheritance at all. So many painful moments of misuse, overuse and high coupling and a deep maintenance hell. worst:people think they rock...


If you want to stick with just Java, Eclipse can generate the boiler plate for delegation to an object in a couple of clicks.


Is it bad that the first thing I saw in his example is void methods returning strings and immediately scoffed and closed the window?


More evidence that if a language lacks macros, people will find an alternate mechanism for language extension (bytecode generation) which is usually much more complex than macros would have been.

On the other hand, reflection in the hands of over-eager users is already a nightmare... perhaps a huge speed bump in the way of full metaprogramming is a good thing for most projects.

As far as the actual application presented here.. it doesn't quite get you Scala's traits (no self-types) but it seems pretty close. Nice hack.


I'm always wondering whether HN people think this kind of experimenting is a good idea or not.


Experimenting is a great idea, that's how we learn, but using your experiments in a production environment is a completely different matter, and usually a bad idea.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: