Is it a feature abuse to introduce default methods in new interfaces?

Joseph D. Darcy joe.darcy at oracle.com
Fri Feb 22 01:15:33 UTC 2019


Hello,

FWIW, I wouldn't necessarily consider default methods on a new interface 
an abuse. The existence of default methods on interfaces changes the 
trade-offs between having an interface paired with an abstract helper 
implementation vs an interface with default methods.

HTH,

-Joe


On 2/21/2019 6:54 AM, Daniel Fuchs wrote:
> Hi Jaikiran,
>
> I don't think this is an abuse, and I'd say that the decision on
> whether to provide a default implementation for a new interface
> probably depends on how well you estimate that the default
> implementation will match what concrete subclass would have to
> implement. Even if that's not an authoritative answer ;-)
>
> I don't know if that's representative but Stream.Builder for
> instance, came with a default add( ) method:
>
> https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.Builder.html 
>
>
> best regards,
>
> -- daniel
>
> On 21/02/2019 14:35, Jaikiran Pai wrote:
>> I have been trying to find some authoritative answer on when _not_ to
>> use default methods on interfaces (a feature introduced in Java 8). The
>> official documentation in Java, about this feature, talks about the
>> motivation[1] behind introducing this feature and explains how
>> _existing_ interfaces can be changed to add default methods without
>> breaking binary compatibility:
>>
>> "Default methods enable you to add new functionality to the interfaces
>> of your libraries and ensure binary compatibility with code written for
>> older versions of those interfaces."
>>
>> This and some other docs don't mention whether it's an accepted practice
>> or abuse of default methods feature, when dealing with new interfaces.
>> Is it fine to introduce new interfaces with default methods? I have
>> tried searching the mailing list archives to see if it's discussed
>> previously but my search terms haven't found anything relevant. Any
>> previous authoritative answer, similar to Stuart Mark's reply on
>> stackoverflow (on a different topic) about Optional usage[2], is also
>> good enough.
>>
>> [1] 
>> https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html
>>
>> [2]
>> https://stackoverflow.com/questions/23454952/uses-for-optional/23464794#23464794 
>>
>>
>> -Jaikiran
>>
>>
>



More information about the jdk-dev mailing list