Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

Two interfaces A and B both declare default String hello(). Class C implements both. What happens, and how do you call A's version?

Class C does not compile, because it inherits two unrelated default methods with the same signature and Java refuses to pick one. C must override hello(), and inside the override it can call a chosen version with A.super.hello(). There is no conflict when one interface extends the other (the more specific one wins), or when C inherits hello() from a superclass (the class always wins).

Why this question is asked

Default methods, added in Java 8, let interfaces carry method bodies, and with them came a question C++ programmers know well: what if two parents provide the same method? Interviewers ask this small coding question because it shows whether we know the compiler's actual behaviour and the slightly unusual X.super.m() syntax. In this answer, we will recap default methods, reproduce the compile error, fix it by choosing A's version, and then look at the two situations in which Java resolves the conflict on its own.

1A Quick Recap of Default Methods

A default method is an interface method with a body, marked with the default keyword. Any class that implements the interface gets that method for free and may override it. The feature was added so that interfaces such as Collection could gain new methods, like stream() and removeIf(), without breaking the millions of classes that already implemented them.

Because a class can implement many interfaces, it can now inherit a method body from more than one place. Java has a short set of rules for this, and when none of them gives a single answer, it asks us to decide.

2Reproducing the Conflict

Here are the two interfaces from the question and a class that implements both without doing anything else.

Java
interface A { default String hello() { return "Hello from A"; } }
interface B { default String hello() { return "Hello from B"; } }
class C implements A, B { }
// javac: error: types A and B are incompatible;
// class C inherits unrelated defaults for hello() from types A and B

Notice that the error is reported on C, not on A or B. Each interface is fine on its own. The problem only exists in a class that combines them, because neither A nor B is more specific than the other and Java will not guess. The reason it refuses to guess is safety: if it silently picked the first interface listed, reordering the implements clause would change behaviour.

3Fixing It and Calling A's Version

To make C compile, we override hello(). Inside the override, the syntax InterfaceName.super.method() calls the default from that particular interface.

Java
class C implements A, B {
@Override
public String hello() {
return A.super.hello(); // choose A's behaviour
}
}
System.out.println(new C().hello()); // Hello from A
class Both implements A, B {
@Override
public String hello() {
return A.super.hello() + " and " + B.super.hello();
}
}
System.out.println(new Both().hello()); // Hello from A and Hello from B

Two details are worth knowing. The override must be public, because interface methods are implicitly public and an override cannot reduce visibility. And A.super only works for an interface that C names directly in its implements clause; we cannot reach past it to a grandparent interface. So the choice is always made explicitly, in one place, by the class that created the combination.

4When One Interface Extends the Other

The conflict disappears if B extends A and overrides hello(). Now B is more specific than A, so its version wins, even when C lists both.

Java
interface A { default String hello() { return "Hello from A"; } }
interface B extends A { @Override default String hello() { return "Hello from B"; } }
class C implements A, B { } // compiles
System.out.println(new C().hello()); // Hello from B

The reasoning is that B already saw A's version and replaced it on purpose. Listing A again in C's implements clause adds nothing new, so there is no real choice to make. If B extends A but does not override hello(), there is also no conflict: only A's default exists.

5When a Superclass Provides the Method

The strongest rule is that a method from a class always beats an interface default. If C extends a class that already has hello(), that one is used, whatever the interfaces say.

Java
class Base { public String hello() { return "Hello from Base"; } }
class C extends Base implements A, B { } // compiles, no override needed
System.out.println(new C().hello()); // Hello from Base
Step throughHow Java picks hello() for class C
  1. Does C or a superclass declare hello()?
    YesUse the class method (class wins)NoLook at the interfaces
  2. Is one interface's default more specific?
    YesUse the most specific defaultNoConflict
  3. C must override hello()and may call A.super.hello() or B.super.hello()

3 steps in order. Play them, or step through them one at a time.

Java resolves the easy cases itself and makes us choose in the hard one.

This rule exists for compatibility. When Java 8 added default methods to existing interfaces, classes that already had a method with that name had to keep behaving exactly as before. Making the class win guarantees that adding a default to an interface never changes the behaviour of a class that already implements the method.

6Follow-up questions

Can C call A.super.hello() if C extends a class that implements A?

No. X.super.m() is allowed only for interfaces that the class lists directly in its implements clause. If we need A's behaviour from such a class, we add A to C's implements clause, or move the shared logic into a static helper that both can call.

What if A's hello() is a default and B declares hello() as abstract?

C still fails to compile unless it overrides hello(), because one parent offers a body and the other demands an implementation and neither is more specific. Inside the override, C can still call A.super.hello() to reuse A's body.

Do static interface methods conflict in the same way?

No. Static interface methods are not inherited by implementing classes at all; they are called as A.util() or B.util(). Two interfaces can have static methods with the same name without any conflict.

7Conclusion

In this answer, we saw that a class implementing two interfaces with the same default method fails to compile until it overrides the method, and that A.super.hello() lets it pick A's version or combine both. We also saw the two cases Java resolves on its own: a more specific subinterface wins over its parent, and a method inherited from a superclass always wins over any interface default.

What to say in the interviewAbout 60 seconds aloud

Open with

C will not compile. It inherits two unrelated defaults for hello(), one from A and one from B, and neither interface is more specific than the other, so javac reports that the types are incompatible and makes C decide.

Then, if they want more

To fix it, C overrides hello() as a public method, and inside it calls A.super.hello() to use A's version. That InterfaceName dot super syntax only works for interfaces C lists directly, and C could also combine both by calling A.super and B.super. There are two cases where Java decides on its own.

If B extends A and overrides hello(), B is more specific and its version wins without any conflict. And if C extends a class that already has a hello() method, the class method always wins over any interface default. That class-wins rule is what kept existing code working when Java 8 added defaults to old interfaces like Collection.

About this answer

Last updated
12 November 2026
Versions it names
Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Two interfaces A and B both declare default String hello(). Class C implements both....": https://scrivio-interview-library.netlify.app/java/interfaces-and-abstract-classes/2ae48c1071/