Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default Methods

 
j rab
Greenhorn
Posts: 16
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does Java 7 allow default methods in Interfaces or is this introduced in Java 8? 
 
Tim Cooke
Sheriff
Pie
Posts: 3204
142
Clojure IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a Java 8 thing.

In general practice, I recommend you avoid them.
 
j rab
Greenhorn
Posts: 16
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim
 
j rab
Greenhorn
Posts: 16
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although, I thought with Java 8 Interfaces can now implement static methods so why would default methods be needed? Can you explain this to me?
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may declare static methods in an interface if these methods are associated with classes that implement the interface. These methods are utility methods and an interface is a more appropriate place to define utility methods as opposed to utility classes, for example, all of the static methods defined in the java.util.Collections utility class could have been defined in the java.util.Collection interface.

You may declare default methods in an interface if these methods are associated with instances of classes that implement the interface. These methods allow you to evolve an interface i.e. you can add new methods to an interface without breaking classes that already implement the interface. In pre-Java 8 code, a non-abstract class that implements an interface is forced to implement every method in the interface. In Java 8 code, a non-abstract class that implements an interface is not forced to implement every method in the interface since it can inherit the interface’s default methods without being forced to provide an implementation.
 
j rab
Greenhorn
Posts: 16
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh ok! Thank you Joe for explaining it to me.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
j rab wrote:why would default methods be needed? Can you explain this to me?

Let's add an illustrative code snippet. Imagine you are into card games and decide to create a framework/library/application where anybody can create the deck of his/her favourite card game. So you create an enum Suit, an enum Rank, and a class Card. Next on the list is an interface Deck with some useful methodsAnd you already release this API while you'll develop the code for the GUI. Many people like this library and start developing concrete Deck classes for their favourite card game. And some of these developers provide some feedback to you: it would be nice to have an add method for a single card and one for a single deck. So you alter the Deck interface and add two methodsAnd you release a new version of this API. But what happens if people update to this new version? They all get compiler errors, because the concrete Deck classes don't implement these two new methods. So no backwards compatibility and everybody needs to update their classes. How could you ensure backwards compatibility and assure no code updates are required? Using default interface methods! Here is the improved Deck interface

Hope it helps!
Kind regards,
Roel
 
j rab
Greenhorn
Posts: 16
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Roel.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic