As far as I know, Groovy already has mixins, Scala has traits. C family has multiple inheritance. So why is the new functionality in Java called "extension methods"? Is it just a different name for the same thing, or was there another reason? What are the differences from traits and mixins? what do they add and what do they lack?
Personaly I see them more as "implementation methods" than "extension methods".
Mixins are hailed as interfaces with behavioral reuse, more flexible interfaces, and more powerful interfaces. You will notice all these have the term interface in them, referring to the Java and C# keyword. Mixins are not interfaces. They are multiple inheritance.
Loosely speaking, a mixin is a type that a class can implement in addition to its "primary type" to declare that it provides some optional behaviour. For example Comparable is a mixin interface that allows a class to declare that it its instances are ordered with respect to other mutually comparable objects.
A trait is kind of a “micro interface” that describes some characteristic of a class design that can be found in many different components throughout the system. By referring to the traits instead of the implementing class itself you can keep the system decoupled and modular.
So why is the new functionality in Java called "extension methods"?
AFAIK, This naming come from C#. Virtual extension methods are only for methods whereas mixins and traits apply to fields as well.
Interface evolution via virtual extension methods Note: Brian himself calls them "Defender methods" but I suppose "Virtual extension methods" is more widely recognised.
JVM Language Summit 2011 - Brian Goetz I am pretty sure this is where he talks about why it has the name it does.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With