문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 softwareengineering.stackexchange
scroll top