Mixin vs inheritance

A mixin is typically used with multiple inheritance. So, in that sense, there’s “no difference”.

The detail is that a mixin is rarely useful as a standalone object.

For example, say you have a mixin named “ColorAndDimension”, which adds a color property and width and height.

Now, you could add ColorAndDimension to a, say, Shape class, a Sprite class, a Car class, etc. And they will all have the same interface (say get/setColor, get/setHeight/Width, etc.)

So, in the generic case a mixin IS inheritance. But you can argue it’s a matter of the role of the class in the overall domain as to whether a mixin is a “primary” class or simply a mixin.


Edit — just to clarify.

Yes, a mixin can be considered, in today’s modern lingo, an Interface with an associated Implementation. It really is just plain, old, everyday multiple inheritance using a plain, old, everyday class. It just happens to be a specific application of MI. Most languages don’t give a mixin any special status; it’s just a class that was designed to be “mixed in”, rather than used standalone.

Leave a Comment