Why there was a need to use default methods in Interface
brian.goetz at oracle.com
Sun Mar 31 12:57:59 PDT 2013
Think of "default" as the dual of "abstract". The compiler could just
as easily infer abstract-ness from the lack of a body, but that wasn't
the choice that was made. And we don't seem to mind the "abstract"
keyword in the places it shows up. If the common case in classes is
that methods have bodies, and the common case in interfaces is that they
don't, in both cases, a keyword is used to indicate "this is the other
Note also that the inheritance rules for concrete methods in interfaces
are subtly different from that of classes. Making that syntactically
explicit helps be clear on what is going on.
The goal must be to maximize comprehension for readers of code, not
minimize keystrokes for writers of code. One of the fundamental values
of the Java language is: reading code is more important that writing code.
I am sure some people could (and no doubt will, given a chance) argue
that this makes the code harder to read because it is "noise". There is
never a "right" answer because syntactic choices are subjective; one
man's noise is another man's valuable reminder. (As with all
syntax-related issues, this one has been debated to death, both inside
of and outside of the Expert Group.)
On 3/31/2013 3:18 PM, Mrityunjay Sharma wrote:
> Hi Brian,
> My question is more about on why mark the method as default explicitly,
> when compiler can perform the task for me (marking the implementation in
> interface as default). With this there will be one less keyword which is
> used only for interfaces.
> Thanks & Regards,
> On Mon, Apr 1, 2013 at 12:42 AM, Mohammed Sanaulla
> <sanaulla123 at gmail.com <mailto:sanaulla123 at gmail.com>> wrote:
> I think the query was why to use "default" keyword in the interface
> while providing the implementation. It could have been skipped and
> instead allow the compiler to identify that any implementation in an
> interface would be default method.
> Please correct me if I misinterpreted the question.
> Mohamed Sanaulla <http://blog.sanaulla.info> |
> On 1 April 2013 00:35, Brian Goetz <brian.goetz at oracle.com
> <mailto:brian.goetz at oracle.com>> wrote:
> Interface evolution.
> Interfaces like Collection cannot be added to, because there are
> implementations of Collection out there, so if we added a method to
> Collection, it would render all existing implementations not
> compileable. This means that interfaces like Collection could
> not be
> evolved once published.
> Default methods provide a path to evolving interfaces over time,
> as it
> lets you compatibly add to an interface as long as the
> functionality you
> want can be expressed in terms of existing interface methods.
> On 3/31/2013 2:48 PM, Mrityunjay Sharma wrote:
> > Hi All,
> > I have a question regarding providing the support for adding
> default method
> > in interface.
> > When we are adding an implementation in interface, compiler
> can itself
> > decide whether its an default implementation or not. As far
> as i know we
> > cannot use default methods for any class, instead we can only
> use it in
> > interfaces. Then why there was a need to use default methods.
> > Thanks & Regards,
> > Mrityunjay
More information about the lambda-dev