• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Default Methods

 
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does Java 7 allow default methods in Interfaces or is this introduced in Java 8?  
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a Java 8 thing.

In general practice, I recommend you avoid them.
 
j rab
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Tim
 
j rab
Greenhorn
Posts: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 175
17
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh ok! Thank you Joe for explaining it to me.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 20
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Roel.
 
Beauty is in the eye of the tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic