Interfaces that extend interfaces
An interface may extend one or more other interfaces, and then it inherits their abstract methods and their default methods. A class that implements it signs both contracts. Two interfaces with unrelated default methods for the same signature are the one case the compiler cannot resolve, and the class has to.
An interface that extends an interface
interface A extends Base adds the methods of Base to its own contract. A class that implements A must implement the methods of both, and an object of it is an instance of both types. The relation is the is-a one again, only between interfaces.
An interface can extend several interfaces at once, which no class can do. The combined contract is the union of their methods, and a name that appears in two of them with the same signature is one method, not two.
The diamond of defaults
The problem appears with default methods. If A and B each declare a default hello() and a class implements both, there are two bodies and no reason to prefer one. The compiler refuses the class instead of guessing.
The file Diamond.java:
public class Diamond {
public static void main(String[] args) {
C c = new C();
System.out.println(c.hello());
}
}
interface Base {
default String hello() {
return "base";
}
}
interface A extends Base {
@Override
default String hello() {
return "a";
}
}
interface B extends Base {
@Override
default String hello() {
return "b";
}
}
class C implements A, B {
}
javac -Xlint:all Diamond.java
Diamond.java:28: error: types A and B are incompatible;
class C implements A, B {
^
class C inherits unrelated defaults for hello() from types A and B
1 error
The message names the two interfaces and the method, and the caret points at the class declaration. The compiler has no rule that makes one default win, so the class must supply the answer.
The class that overrides
The fix is one override in C. Inside it, A.super.hello() and B.super.hello() call the two defaults explicitly, so the class can combine them or choose one.
public class Fixed {
public static void main(String[] args) {
C c = new C();
System.out.println(c.hello());
}
}
interface Base {
default String hello() {
return "base";
}
}
interface A extends Base {
@Override
default String hello() {
return "a";
}
}
interface B extends Base {
@Override
default String hello() {
return "b";
}
}
class C implements A, B {
@Override
public String hello() {
return A.super.hello() + " + " + B.super.hello();
}
}
javac -Xlint:all Fixed.java
java Fixed
a + b
A.super.hello() is only legal inside a class that implements A, and it selects that interface default rather than the class override, which would be infinite recursion. The two calls run in the order they are written, and the result is the combination the class chose.
The rules
A class method wins over a default method. Between two defaults from unrelated interfaces, the class must override. A default from a subinterface wins over a default from a superinterface, because it is the more specific one. Those three rules cover every conflict, and the compiler reports the one case that remains.
The diamond in this lesson is not the only shape: an interface may extend three others, and a class may implement all three. The rules are the same at any size, and a hierarchy of interfaces is still a type relation, not the fragile base class problem of inheritance between classes.
Exercises
- Make A and B abstract-only for hello() and check that the conflict disappears.
- Change the override in Fixed.java to return only A.super.hello().
- Add a third interface C3 with its own default and a second conflict, and resolve it in the class.
- Give Base a second method with no default and implement it once in the class.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many interfaces may one interface extend?
Answer
Any number
An interface can extend several interfaces, and a class can implement several as well.
-
When does a default-method conflict happen?
Answer
When a class implements two interfaces with unrelated defaults for one signature
Two bodies for one signature leave no reason to choose, so the class must override.
-
What does
A.super.hello()select?Answer
The default method of interface A
It names the interface whose default to call, avoiding the class override.
-
What does this program print?
public class Q1 { interface A { default String text() { return "a"; } } interface B extends A { default String text() { return "b"; } } static class C implements B { } public static void main(String[] args) { C c = new C(); System.out.println(c.text()); } }Answer
b
The more specific default from B wins over the one inherited from A.
-
What does this program print?
public class Q2 { interface A { String text(); } interface B extends A { String other(); } static class C implements B { public String text() { return "t"; } public String other() { return "o"; } } public static void main(String[] args) { B b = new C(); System.out.println(b.text() + b.other()); } }Answer
to
B inherits text from A and adds other, and the class implements both.
-
What is the first line of the compiler output for this program?
public class Q3 implements A, B { } interface A { default String hello() { return "a"; } } interface B { default String hello() { return "b"; } }Answer
error: types A and B are incompatible;
The two unrelated defaults conflict, and the compiler names both interfaces.
-
What does this program print?
public class Q4 { interface A { default String text() { return "a"; } } interface B { default String text() { return "b"; } } static class C implements A, B { @Override public String text() { return A.super.text(); } } public static void main(String[] args) { C c = new C(); System.out.println(c.text()); } }Answer
a
The class override resolves the conflict and calls the A default.
-
Which wins when a class method and a default method have the same signature?
Answer
The class method
A class implementation always wins over an interface default.
-
What does this program print?
public class Q5 { interface A { default String name() { return "a"; } } static class C implements A { @Override public String name() { return "c"; } } public static void main(String[] args) { A a = new C(); System.out.println(a.name()); } }Answer
c
The class override is the implementation, and the interface reference calls it.
-
Why can the diamond of defaults not be resolved by the compiler?
Answer
Both defaults are equally specific and unrelated
No rule prefers one unrelated default, so the choice belongs to the class.
The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.
Article author: Arthur Isaev