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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  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?

Mohammad Nadeem user avatar by
Mohammad Nadeem
·
Mar. 25, 19 · Analysis
Like (4)
Save
Tweet
Share
22.63K 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

  • Multi-Cloud Integration
  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • How To Handle Secrets in Docker
  • 10 Best Ways to Level Up as a Developer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: