DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > When to Use Java 8 Default Methods in Interfaces

When to Use Java 8 Default Methods in Interfaces

When should you use Java 8 default methods?

Mohammad Nadeem user avatar by
Mohammad Nadeem
·
Mar. 25, 19 · Java Zone · Analysis
Like (4)
Save
Tweet
21.73K Views

Join the DZone community and get the full member experience.

Join For Free

One of the Oracle’s articles describes in great detail when Default methods should be used:

The section Interfaces describes an example that involves manufacturers of computer-controlled cars who publish industry-standard interfaces that describe which methods can be invoked to operate their cars. What if those computer-controlled car manufacturers add new functionality, such as flight, to their cars? These manufacturers would need to specify new methods to enable other companies (such as electronic guidance instrument manufacturers) to adapt their software to flying cars. Where would these car manufacturers declare these new flight-related methods? If they add them to their original interfaces, then programmers who have implemented those interfaces would have to rewrite their implementations. If they add them as static methods, then programmers would regard them as utility methods, not as essential, core methods.

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.

Here is what we can deduct:

  • Default method (Defender methods) should be used for backward compatibility. Whenever you want to add additional functionality in an existing interface, you can use default methods without breaking any existing implementing classes (Default methods enable us to add new functionality to existing Interfaces without breaking older implementation of these Interfaces).
  • Default methods in interfaces were introduced to the Java language in order to evolve the collections library for the Streams API.
  • The idea is not to design your solution with default methods in mind, but rather, they are an afterthought.

Java has long history of being backward compatible:

  • With backward compatability in mind, Java generics are a Type Erasure instead of reifiable Type
  • With backward compatability in mind, we still find AWT.
  • With backward compatability in mind, we still have Vector (event thought we have Arraylist and CopyOnWriteArrayList)
  • And then, with backward compatability in mind, default methods are introduced.

Why Default Methods

It was a smart move to evolve the existing API (collection, for example) and to introduce new programming models (like functional) without breaking a zillion lines of code. For example:

  • java.util.Collection — this interface now has methods related to Stream while, at the same time, being compatible with existing version.
  •  java.lang.Iterable — this interface now supports functional programming.
Interface (computing) Java (programming language)

Published at DZone with permission of Mohammad Nadeem, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Hashtable, HashMap, ConcurrentHashMap: Performance Impact
  • DZone's Article Submission Guidelines
  • Don't Underestimate Documentation
  • Evolving Domain-Specific Languages

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo