8

This post describes a new feature in Java 8 called virtual extension methods (formerly called default methods, or defender methods). In the example provided, an interface has one method, which is implemented by a class. Then a second method is added, but instead of forcing the class to immediately implement the interface's new method, a "virtual extension method" is created within the interface as an implementation of this new method, thus the class doesn't need to implement the new method right away.

But say that the programmer decides to create a virtual extension method in an interface and never plans to have subclasses implement its own version. Wouldn't that be a mixin? It seems like one to me, but I've never heard it referred to as such. Is there some fundamental difference that I'm not understanding?

Thunderforge
  • 2,668
  • 3
  • 23
  • 30

2 Answers2

7

Yes, these are mixins. They aren't as powerful as Scala traits (no ability to have a reference to the parent "self" object and its properties or methods), but the default methods can implement meaningful behaviour and so can be more more than sticking plasters to stop existing code breaking when an interface is extended.

The limited power of default methods seems to be deliberate, judging by the various informed discussions of the feature. The designers' advice is that default methods should generally not contain the concrete implementation of a method but accept a lambda or reference to a concrete class, either of which can provide the implementation. Example here

itsbruce
  • 3,195
  • 17
  • 22
2

It's similar to a mix-in but not as powerful since an interface can't contain data.

kevin cline
  • 33,608
  • 3
  • 71
  • 142
  • 4
    Why do you say *similar* to a mix-in? Ruby's implementation is not *the* canonical absolute version, just *a* version. – itsbruce Nov 19 '13 at 17:59
  • 4
    And there's nothing about any definition of mixins that I've seen which *requires* that state be inheritable as well as behaviour. If there were, then no, Java 8 interfaces wouldn't be mixins, because they *deliberately* exclude multiple inheritance of state. – itsbruce Nov 19 '13 at 18:59
  • Wikipedia says they can store data, but there's no citation to back it up. I've created [this question](http://programmers.stackexchange.com/q/218936/81973) to get an answer about whether or not a mixin by definition can do this. – Thunderforge Nov 19 '13 at 21:00
  • @Thunderforge Which bit of the Wikipedia page makes you think fields are part of the definition? It's an unsatisfactory page, but it describes several early implementations of which one (Simula) enforces a rigid separation between fields and methods, with mixins providing the latter. – itsbruce Nov 19 '13 at 21:33
  • There is no mix-in concept in Java 8. Don't know why You down-voted Kevin. Java 8's default interface methods are one step into the right direction. True mix-ins require properties. – Hartmut Pfarr Jul 28 '15 at 12:20
  • Since Java interfaces can't hold data, tasks commonly done with mix-ins in other languages can't be done via interface inheritance in Java. Ergo, to programmers used to writing mix-ins in other languages, Java does not support mix-ins. – kevin cline Jul 29 '15 at 17:12