DZone
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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Advanced Brain-Computer Interfaces With Java
  • Simplify Java: Reducing Unnecessary Layers and Interfaces [Video]
  • Java 21 SequenceCollection: Unleash the Power of Ordered Collections
  • Projections/DTOs in Spring Data R2DBC

Trending

  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • Batching Strategies: Throughput vs Latency in Practice
  • Java 27 Makes G1 the Default Everywhere: How to Test Your Small Containers Before You Upgrade
  • PII Detection Before RAG Ingestion: Screening Documents in C#

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. 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?

By 
Mohammad Nadeem
·
Mar. 25, 19 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
25.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Advanced Brain-Computer Interfaces With Java
  • Simplify Java: Reducing Unnecessary Layers and Interfaces [Video]
  • Java 21 SequenceCollection: Unleash the Power of Ordered Collections
  • Projections/DTOs in Spring Data R2DBC

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook