Default methods were added in Java 8 mainly to support lambda expressions. Designers (in my opinion, cleverly) decided to create lambda expression syntax to create anonymous interface implementations. But these lambdas can implement only one method, they will be limited to interfaces with one method, which will be a pretty serious limitation. Instead, default methods have been added to allow more sophisticated interfaces.
If you need some convincing assertion that default was introduced because of lambdas, note that the 2009 proposal by Mark Reinhold, the Project Lambda straw offer , mentions “extension methods” as a required feature added to support lambdas.
Here is an example demonstrating the concept:
interface Operator { int operate(int n); default int inverse(int n) { return -operate(n); } } public int applyInverse(int n, Operator operator) { return operator.inverse(n); } applyInverse(3, n -> n * n + 7);
Very far-fetched, I understand, but I have to show how, by default supports lambdas. Since inverse is the default, it can easily be overridden by the implementing class if required.
sprinter Jul 23 '15 at 5:55 2015-07-23 05:55
source share