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

  • MCP Is Coming to Your Desktop Applications
  • Beyond SOLID: Embracing CUPID for Modern Software Craftsmanship
  • Beyond Conversation: Mastering Context with Claude Code Skills and Agents
  • Clean Code: Interfaces in Go — Why Small Is Beautiful, Part 3

Trending

  • Hot-Reloading Kubernetes Secrets and ConfigMaps in Go With mamori (No Pod Restarts)
  • Why Incident Response Needs Memory, Not Just Intelligence
  • Build Software Faster With Three Simple Principles
  • Putting Guardrails Around What Your AI Agent Is Allowed to Touch

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.

Default and Static Methods in Interfaces

This overview of both default and static methods covers why default methods are necessary with interfaces and how they interact with inheritance and multiple inheritance.

By 
Gaurav Rai Mazra
·
Updated Feb. 09, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
49.8K 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

Java 8 introduced default and static methods in interfaces. This feature enables us to add new functionality in the interfaces without breaking the existing contract of the implementing classes.

How to Define Default and Static Methods?

Default methods have default, and static methods have thestatic keyword in the method signature.

public interface Parser {
  List<String> parse(File file);

  // default implementation valid for all the file parser viz. JsonFileParser, CsvFileParser, TextFileParser
  default boolean canParse(File file) {
    return Objects.nonNull(file) && file.getName().endsWith(getFileType().getExtension());
  }

  FileType getFileType();

  static void someStaticHelperMethod() {
    //helper method implementation 
  }
}

What Is the Need for Default Methods?

Default methods in interfaces help us to introduce new functionality without breaking the contract of the implementing classes.

Suppose we have an Expression interface that has ConstantExpression, BinaryExpression, DivisionExpression etc. as existing implementations. Now we get a requirement to add new functionality.

  • Return the signum of the evaluated result.

  • Return signum after evaluating the expression.

This can be done with default and static methods without breaking any functionality as follows.

public interface Expression {
  double evaluate();

  default double signum() {
    return signum(evaluate());
  }

  static double signum(double value) {
    return Math.signum(value);
  }
}


You can find the full example code on GitHub.

Default Methods and Multiple Inheritance Ambiguity Problems

Java supports multiple inheritance of interfaces. Consider having two interfaces, InterfaceA and InterfaceB , having default methods with the same signature. Your class ConcreteC is implementing both the interfaces.

interface InterfaceA {
  void performA();
  default boolean canPerform() {
    // return true if I can perform the action
  }
}

interface InterfaceB {
  void performB();
  default boolean canPerform() {
    //return true if I can perform the action
  }
}

class ConcreteC implements InterfaceA, InterfaceB {

}


The above code will fail to compile with "error: unrelated defaults for canPerform()from InterfaceA and InterfaceB."

To overcome this problem, you need to override the default method.

class ConcreteC implements InterfaceA, InterfaceB {
  override
  public boolean canPerform() {

  }
}


But say you don't want to provide the implementation of the overridden default method but instead want to reuse the existing one. That is also possible with the following syntax.

class ConcreteC implements InterfaceA, InterfaceB {
  override
  public boolean canPerform() {
    return InterfaceA.super.canPerform();
  }
}

A Few Important Points for Default Methods

  • You can inherit the default method.

  • You can redeclare the default method essentially making it abstract.

  • You can redefine the default method (equivalent to overriding).

Interface (computing)

Opinions expressed by DZone contributors are their own.

Related

  • MCP Is Coming to Your Desktop Applications
  • Beyond SOLID: Embracing CUPID for Modern Software Craftsmanship
  • Beyond Conversation: Mastering Context with Claude Code Skills and Agents
  • Clean Code: Interfaces in Go — Why Small Is Beautiful, Part 3

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