Link Details

Link 13017 thumbnail
User 211189 avatar

By rasman
via erik-rasmussen.com
Published: Feb 07 2007 / 14:02

A clever trick using interfaces, static inner classes, and a pattern similar to a "mixin" to achieve something similar to multiple inheritance. The main goal of this trick is to reduce code duplication and increase code maintainability.
  • 9
  • 0
  • 2881
  • 680

Comments

Add your comment
User 179375 avatar

Ricky Clarkson replied ago:

0 votes Vote down Vote up Reply

I'm not sure why you would use this over ordinary static methods in separate utility classes.

User 191976 avatar

Ganeshji Marwaha replied ago:

0 votes Vote down Vote up Reply

@rickyclarkson - u got a point here. with java 5's static imports it is easier than ever. You wont get overriding or polymorphism, but that is the case for any workaround in this area.

User 102747 avatar

dc103345 replied ago:

0 votes Vote down Vote up Reply

Urgh, I just don't like this sort of thing. Agreed we need a solution for the problem but it's not multiple inheritance or a poor man's implementation of that concept - remember the diamond of death! Fundamentally, multiple inheritance encourages mixing reuse of implementation with subtyping ("something is like something else").

The typical solution to this in Java's case is to define interfaces and then have abstract classes which implement those interfaces. We then create a class to implement all our chosen interfaces and have it delegate out to concrete implementations of those abstract classes (or from scratch implementations) to get our "mixin" type behaviour. This is not a new approach, it's mention in GoF for example.

In summary, beware of cases where you use inheritance to mix together both type relationships and common implementation. That's putting two tensions on a single mechanism - a bad idea in my book because you can never satisfy both of them completely.

User 202224 avatar

deconcoder replied ago:

0 votes Vote down Vote up Reply

On the other hand, the opposite is equally noxious: the total denial of all typing and the insistence on delegating everything. I had an 'architect' recently tell me that a Customer class inheriting from a User was not a good design, because User should just have Customer information. Such a principle would certainly make the number of classes in the JDK's containers package drop (to 1).

User 189024 avatar

kieronwilkinson replied ago:

0 votes Vote down Vote up Reply

"The lack of multiple [class] inheritance in Java is often considered one of its biggest flaws."

Is that really true? In my 10 years of Java development, I have never needed multiple class inheritance that I couldn't otherwise represent using multiple interface inheritance and a few utility methods. I actually consider the concept horrible, and I'm glad Java doesn't have it. I think that the lack of multiple class inheritance in Java has saved a lot of people a lot of pain. Inheritance taken to far is bad, multiple class inheritance would make that even worse.

Surely I am not alone?

User 179375 avatar

Ricky Clarkson replied ago:

0 votes Vote down Vote up Reply

No, the biggest flaw is that it supports class inheritance at all.

The way Scala does mixins, basically a compile-time 'view' of an object plus the methods from another object/class, makes inheritance-for-convenience obsolete. Perhaps Java would benefit from borrowing that.

Add your comment


Html tags not supported. Reply is editable for 5 minutes. Use [code lang="java|ruby|sql|css|xml"][/code] to post code snippets.

Voters For This Link (9)



Voters Against This Link (0)