Interface defaults and conflict rules
What you will learn
A default method provides interface behavior that implementing classes may inherit or override. When unrelated interfaces offer competing implementations, Java generally requires an explicit decision rather than choosing an arbitrary winner.
After this lesson you can:
- Use default, static and private interface methods in their correct roles.
- Resolve a conflict between two inherited default methods.
Use a separate Main.java for each example. Compile with javac Main.java and run with java Main. The code is compatible with Java 21; Java 25 execution is not yet verified.
interface Badge { String name(); default String display() { return decorate(name()); } private String decorate(String value) { return "[" + value + "]"; } static String category() { return "Badge"; }}class Visitor implements Badge { public String name() { return "Visitor"; }}public class Main { public static void main(String[] args) { Badge badge = new Visitor(); System.out.println(badge.display()); System.out.println(Badge.category()); }}Expected output:
[Visitor]BadgeThe default calls the implementation of name and then its own private helper. The static operation is invoked on Badge.
Resolve competing implementations deliberately
Badge.display() is a public instance method with a body. Visitor supplies name() and receives display() through inheritance. The private decorate() helper is internal to Badge; implementing classes do not inherit it. The static category() method belongs to Badge and is called as Badge.category(), not Visitor.category(). Default and static interface methods arrived in Java 8; private interface methods arrived in Java 9.
For the ordinary public instance methods used here, a compatible concrete superclass implementation takes precedence over an interface default. A more specific subinterface override takes precedence over an inherited parent default. If two unrelated interfaces contribute conflicting defaults with the same signature, implement an explicit override. In Combined, ShortText.super.text() and LongText.super.text() explicitly select the direct superinterface implementations. You may instead provide a completely new implementation, provided both contracts are honored. Qualified super is subject to direct-superinterface and specificity restrictions; it cannot freely select an arbitrary ancestor.
The simple priority rules assume compatible signatures and accessibility. An abstract superclass declaration may suppress an interface default and leave an implementation obligation; an unrelated abstract/default conflict can also need an explicit declaration. Two inheritance paths to the same original default are not by themselves a conflict. An interface cannot provide a default that overrides a non-private Object method such as toString(). Use default methods for a genuine shared contract, not as a substitute for object state or arbitrary code reuse.
interface ShortText { default String text() { return "Short"; } }interface LongText { default String text() { return "Long"; } }class Combined implements ShortText, LongText { @Override public String text() { return ShortText.super.text() + "/" + LongText.super.text(); }}public class Main { public static void main(String[] args) { Combined combined = new Combined(); ShortText a = combined; LongText b = combined; System.out.println(a.text()); System.out.println(b.text()); }}Expected output:
Short/LongShort/LongThe class override is used through both interface references, and combines the two selected defaults.
Check a boundary or variation
interface Basic { default String name() { return "Basic"; } }interface Detailed extends Basic { @Override default String name() { return "Detailed"; }}class Card implements Detailed {}class Named { public String name() { return "Class"; } }class NamedCard extends Named implements Detailed {}public class Main { public static void main(String[] args) { System.out.println(new Card().name()); System.out.println(new NamedCard().name()); }}Expected output:
DetailedClassThe subinterface supplies the more specific default for Card; NamedCard inherits the compatible public class method instead.
Common mistakes
Do not assume the first interface in an implements list wins. Reordering interfaces does not resolve unrelated default conflicts. Provide an override that deliberately fulfills both contracts.
Practice
Define Header and Footer interfaces, each with a default label() returning its name. Page implements both and returns Header + Footer by explicitly calling both defaults. Check Page directly and through each interface reference.
Add a public overriding label() in Page. Call Header.super.label() and Footer.super.label() in that method.
Next: Checked and unchecked exceptions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Interface defaults and conflict rules — knowledge check
Apply the stated Java 21-compatible contracts and predict observable behavior.
What does default on an interface method provide?
How do you call Badge.category()?
Two unrelated defaults conflict. What resolves it?
Which method wins in NamedCard?
Can implementing classes inherit Badge.decorate()?