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
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
  1. DZone
  2. Coding
  3. Java
  4. Java 8: Methods in Interfaces

Java 8: Methods in Interfaces

Java has embraced multiple inheritance in interfaces, but in more recent versions, that's caused trouble for API designers. Here's how to order your methods properly.

Martin Farrell user avatar by
Martin Farrell
·
Apr. 26, 17 · Tutorial
Like (22)
Save
Tweet
Share
19.39K Views

Join the DZone community and get the full member experience.

Join For Free

My previous post on streams demonstrates how useful this feature is to Java 8. However, it created a problem for the API designers. The problem was how to we extend the existing Collection’s API without breaking existing Collections implementations (Guava, Apache, etc.).

The solution was to allow methods in interfaces, meaning that any implementations already carry an implementation of the extension.

A good example is:

public interface Collection extends Iterable {
    // ....
    default Stream<E> stream() {
        return StreamSupport.stream(spliterator(), false);
    }
    // ....
}


Rules on default methods

  • Always public
  • default – keyword

Multiple Inheritance

Java always had multiple inheritance in interfaces, but it wasn’t an issue, as it didn’t matter which version of the inherited method was implemented.

interface Fred {
    void run();
}
public class OldSkoolMultipleInheritance implements Runnable, Fred {
    @Override
    public void run() {
        System.out.println("Runnable and Fred share the method signature run()");
    }
}


It’s more complex when default methods are involved, and Java 8 has an order of precedence:

  • classes > interfaces
  • children > parent
  • else select or override implementation

Examples

interface Interface1 {
    default void defaultMethod() {
        System.out.println("print me!");
    }
}
interface Interface2 {
    default void defaultMethod() {
        System.out.println("no print me!");
    }
}


  • Classes > Interfaces:
public class DefaultMethodMultipleInheritance implements Interface1, Interface2 {
    @Override
    public void defaultMethod() {
        System.out.println("I win");
    }
}


  • Child Interfaces > Parent Interfaces:
interface Interface3 extends Interface2 {
    default void defaultMethod() {
        System.out.println("No no print me!!!");
    }
}
public class DefaultMethodMultipleInheritance implements Interface2, Interface3 {
    public static void main(String[] args) {
        new DefaultMethodMultipleInheritance().defaultMethod();
    }
}


Output - No no print me!!!


  • Re-implement method in the DefaultMethodsClass and call the specific implementation using super:
Interface1.super.newWave(); or Interface2.super.newWave();


For example:

public class DefaultMethodMultipleInheritance implements Interface1, Interface2 {
    @Override
    public void defaultMethod() {
        Interface1.super.defaultMethod();
    }
}
[/cpde]


Static Methods

The reasoning here is to keep static methods specific to your interface in one location:

  • Public.
  • Static.
  • Never inherited.
Interface (computing) Java (programming language)

Published at DZone with permission of Martin Farrell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevSecOps Benefits and Challenges
  • Core Machine Learning Metrics
  • The Real Democratization of AI, and Why It Has to Be Closely Monitored
  • Hackerman [Comic]

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: