Question

I'm reading about mixins in ruby, and am reflecting on some of the knowledge I have about java. Isn't a mixin just another member variable that responds to methods of that type? I understand that in languages like ruby, its just a set of methods that can be "Mixed in" to the class. But how is that different from a java class containing a class member variable?

Thank you in advance.

Was it helpful?

Solution

I'd say Ruby mixins are more similar to Java interfaces than Java class variables.

Similar to Java, Ruby does not have multiple inheritance. In both languages a class can only have a single parent class (although that parent, can have it's own parent, and so on).

Similar to how Java uses interfaces to declare functions that all implementing classes should define, Ruby uses mixins to enhance the functionality of a given class hierarchy.

Java instance variables are defined within the class, while Ruby mixins are like adding a new parent class in between your current class and its parent. To objects of the class, yeah mixins might seem similar to Java instance variables (in the same way that an object doesn't know the difference between what members it receives from its instantiating class and what members were inherited from parent classes).

Maybe the important distinction is that Ruby mixins are added to the inheritance hierarchy while adding Java instance members requires modifying the actual class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top