I've been a zone leader with DZone since 2008, and I'm crazy about community. Every day I get to work with the best that JavaScript, HTML5, Android and iOS has to offer, creating apps that truly make at difference, as principal front-end architect at Avego. James is a DZone Zone Leader and has posted 639 posts at DZone. You can read more from them at their website. View Full User Profile

Have Your Say on Java 8 Default Methods Syntax

08.08.2012
| 20278 views |
  • submit to reddit

Brian Goetz has just published a one question survey where you can indicate your preference on the syntax to be used for default methods in Java 8. 

Default methods will be used to allow an interface method to provide an implementation used as default in the event that no concrete class provides an implementation for that method.  The choice presented is fairly subtle, where the default keyword is specified either at the start or end of the method signature. 

If you're fussy about syntax, here's a good opportunity to have you say.

 

 

Reference: https://www.surveymonkey.com/s/9VPJZQQ
Tags:

Comments

Jonathan Fisher replied on Wed, 2012/08/08 - 1:38am

Just a question... Putting a default default implementation: A. Will only? B. Should only? be done when there is an inheritance conflict...

A or B or Both or I don't understand the purpose of this yet.

EDIT:

Watch this video: http://medianetwork.oracle.com/video/player/1113272518001

Java, for better or worse, has 100% backward compatibility. Essentially, this allows you to extend an interface, but people that depended on the old version of the interface would still be able to compile their code without having to implement any new methods. 

Gervais Blaise replied on Wed, 2012/08/08 - 2:42am

Instead of the fact that it is on an Interface, what is the difference between a default method and a method on an abstract class ?

If an interface must provide an implementation, I refactor it to an abstract class. If the interface is required for inheritance purpose I create a support class with the default method implementation and an abstract class.

 My opinion is that the survey is missing an option "Nothing of them, please focus your effort on more important things"

 

Jan Gaspar replied on Wed, 2012/08/08 - 5:32am

Is the 'default' keyword actually needed? I think it is clear that if a method has an implementation within an interface definition then it must be default. Or am I missing something?

Jonathan Fisher replied on Wed, 2012/08/08 - 9:40am in response to: Gervais Blaise

@Gervais Blaise

That was my reaction as well. I don't like it either, but let me demonstrate why they're doing it...

Say you implement comparable in jdk5. Jdk6 comes about and they add a method to comparable. Your code still runs. However, if you recompile your code, all of the sudden you're missing a method in the interface and your code doesn't compile. Their solution is to allow the interface to provide a default method in case your code doesn't provide one.

I agree though, this is far from an ideal solution... I would have rather seen Project Jigsaw than more language sugar.

Rogerio Liesenfeld replied on Wed, 2012/08/08 - 4:20pm in response to: Jonathan Fisher

Jigsaw will be useful (in Java 9), but default methods are a must if you really want to take advantage of lambdas in Java 8.

For example, it will allow a "filter(...)" method (among others) to be added to java.util.Collection. With that, the following code can be written: 

    List<People> adults = people.filter(p -> p.getAge() >= 18);

IMO, lambdas and default methods will have a much bigger impact (for the better) than module support.

Peter Stricker replied on Wed, 2012/08/08 - 4:35pm

Consider this interface, which can increment a value or set it explicitly:

<code>

public interface Incrementer {

  void increment();

  public int getValue();

  void setValue(int);

}

</code>

This class keeps track of how often the value is set explicitly:

<code>

public class Counter implements Incrementer {

  private int value = 0;

  private int resets = 0;

  public void increment() { value++; }

  public int getValue() { return value; }

  public void setValue(int value) { this.value = value; reset(); }

  private void reset() { resets++; }

  public int numResets() { return resets; } // returs number of times value has been set explicitly

}

</code>

Now, the jar which defines the interface is updated:

<code>

public interface Incrementer {

  void increment();

  public int getValue();

  void setValue(int);

  void reset() default { setValue(0); }

}

</code>

What happens now, when you call new Counter().setValue(10); ?

Does the new public default implementation of reset() hide the existing private implementation, or does ot just make it publicly available?

Is reset() now callable from outside Counter?

Will Counter compile?

What is the output of this snippet?

<code>

Incrementer counter = new Counter();

counter.increment();

counter.reset();

System.out.println(counter.getValue());

</code>

 

Suk-hyun Cho replied on Thu, 2012/08/09 - 2:12am in response to: Peter Stricker

Adding a default method does not break backwards compatibility. Method definition in the implementation always takes precedence before any default methods are considered.

Suk-hyun Cho replied on Thu, 2012/08/09 - 2:18am in response to: Gervais Blaise

I lost the link to the presentation by Brian Goetz that explains this in great detail, but in short, this is necessary to enhance behavior on existing library (Collections being an important one) without breaking backwards compatibility when JDK 8 introduces closures. For example, they might add a Collection.map() default method that takes a closure and applies it for every element, which would be much better than providing Collections.map() that takes a Collection and a closure.

Giving the power of closures without any libraries to support it would be almost as bad as giving a library to support this cool new feature while breaking the world.

Christian Kulen... replied on Sun, 2012/08/12 - 2:52pm in response to: Jan Gaspar

Is the 'default' keyword actually needed? I think it is clear that if a method has an implementation within an interface definition then it must be default. Or am I missing something?

Thank you for pointing this one out! Totally true.

Liam Knox replied on Wed, 2012/08/15 - 6:33pm

the idea of default I guess is to allow interface extension

this should be done by explicit default keyword prior 

Saurabh Jain replied on Fri, 2014/04/04 - 10:03pm

Whoa these days lots of buzz is going on default methods , once java 8 is officially release it i am sure that most of the applications will definitely going to use it.

The reason why i think till now there was always a constraint that if we want to introduce some new method (prior to java 8) in an interface ,then it can break the whole application as all the implementation will be forced to provide an implementation for that ,so in that case we try to solve this problem by introducing a new interface to include that new method and i think here we violate the Single Responsibility Model (as its not stated anywhere).

Again I personally feel that Abstract classes have their own cost of using them in terms of multiple inheritance and I also feel that Default methods and abstract classes have there own place in Java and they can't replace each other.

One more thing I found an interesting interesting article on Default methods here , it is little bit on the sarcastic side but a worth reading.

http://lotusmediacentre.com/default-methods-in-java-8-explained/

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.