Is there a way to emulate mixins or traits in java? basically, I need a way to do multiple inheritance so I can add common business logic to several classes
8 Answers
Not the way you want to do it. Effective Java recommends that you "Favor composition over inheritance". Meaning you move the common logic to other classes and delegate. This is how you get around the lack of multiple inheritance in java.

- 60,628
- 22
- 121
- 123
I would encapsulate all of the business logic into a new class BusinessLogic
and have each class that needs BusinessLogic
make calls to the class. If you need a single rooted heirarchy for your classes that make calls to BusinessLogic
, you'll have to create an interface as well (BusinessLogicInterface
?)
In pseudo-code:
interface BusinessLogicInterace
{
void method1();
void method2();
}
class BusinessLogic implements BusinessLogicInterface
{
void method1() { ... }
void method2() { ... }
}
class User
extends OtherClass
implements BusinessLogicInterface
{
BusinessLogic logic = new BusinessLogic();
@Override
void method1() { logic.method1(); }
@Override
void method2() { logic.method2(); }
}
This isn't the prettiest implementation to work around a lack of multiple inheritance and it becomes quite cumbersome when the interface has a lot of methods. Most likely, you'll want to try and redesign your code to avoid needing mixins.

- 3,091
- 1
- 27
- 42

- 24,678
- 14
- 64
- 87
-
1ok, so there are no tricks to avoid having to use delegation and invoking logic.method1, logic.method2, logic.method3 explicitly? it would help save on typing and boilerplate code.. – joshjdevl Nov 04 '08 at 20:48
-
1@joshjdevl There are, but it means [hacking the compiler](http://projectlombok.org/features/Delegate.html). – maaartinus Jun 30 '12 at 14:42
-
Lombok's @Delegate: https://projectlombok.org/features/Delegate.html – Marcin Orlowski Dec 30 '21 at 14:55
Is the object-purist stirring in you today?
Think you could do with a little composite oriented programming?
Then you, sir, are looking for Apache Polygene (formerly named Qi4J, then it renamed to Zest and/or Apache-Zest) ;)
Update 2022; It's discontinued currently, but useful anyway.

- 7,611
- 5
- 39
- 71

- 8,642
- 3
- 35
- 43
-
Does it support traits or just mixins? I.e. when there is a conflict b/w two methods with identical signatures, does one win out, or does it show up as a compiler error and give you some way to resolve the conflict? – Neil Traft Mar 07 '11 at 21:59
-
@NeilTraft method resolution is predictible and is based on declaration orders, see http://qi4j.org/_core.html#core-api-mixin – eskatos Sep 06 '12 at 12:42
You can exploit the fact that interfaces allow nested classes (automatically public static) to keep the default implementation of the interface methods encapsulated within the interface itself. I.e. move the BusinessLogic class of Alex B's example inside the interface.
This is similar to the way Scala generates the JVM code for traits as explained here How are Scala traits compiled into Java bytecode?
When doing this the example becomes:
interface BusinessLogicInterface {
void method0();
class DefaultImpl {
private DefaultImpl() {
}
public static void method1(BusinessLogicInterface self) { ... }
public static void method2(BusinessLogicInterface self) { ... }
}
void method1();
void method2();
}
class User extends OtherClass implements BusinessLogicInterface {
@Override
void method0() { ... }
@Override
void method1() { BusinessLogic.defaultImpl.method1(this); }
@Override
void method2() { BusinessLogic.defaultImpl.method2(this); }
}
Note that we pass an object of the interface type as the "self" parameter. This means the business logic can use other abstract methods (method0). This can be very useful for creating a trait with abstract methods that are all orthogonal to each other and utility "extension" methods that may be implemented in terms of these orthogonal methods.
The drawback is that each interface must copy/paste the boilerplate delegation code. Another often used pattern in Java without this drawback (but with less cohesion and less OO way to call the methods) is to create a class with the plural name as the interface containing the static methods, this is used in the Collections utility class.

- 1
- 1

- 1,495
- 2
- 15
- 25
-
1Note that Java 8 will contain "virtual extension methods" which will enable exactly much more easily. – Henno Vermeulen Nov 07 '13 at 15:13
Java's answer to multiple inheritance is the ability to implement multiple interfaces. Of course, this means you'll get the method declarations, but not the logic.
You could try emulating mixins by composition: your Java class could define member variables that represent other classes that perform some common business logic.
In designing Java classes, I have not found the lack of C++ style multiple inheritance to inhibit the design of my architecture. You will find a way to achieve what you want to do.

- 20,726
- 23
- 93
- 130
As of Java-8, default interface methods were added. This, together with multiple inheritance of interfaces in Java should allow some sort of mixin. Clearly the interfaces have to operate independently. So, there will be significant limitations.

- 101
- 1
- 1
- 5