Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 extension methods - why are they not called mixins or traits? [closed]

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".

like image 939
MartinTeeVarga Avatar asked Jun 11 '13 05:06

MartinTeeVarga


People also ask

Are Mixins like interfaces?

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.

What are Mixins in Java?

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.

What is trait in Java?

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.


1 Answers

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.

like image 193
Peter Lawrey Avatar answered Oct 26 '22 01:10

Peter Lawrey