Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

Explain the three rules Java uses to resolve which method is inherited when classes and interfaces provide the same method, and why Java avoids the C++ diamond problem.

Java resolves a method inherited from several places with three rules in order: a method from the class or any superclass wins over every interface default; otherwise the most specific interface wins, a subinterface over its parent; otherwise the class must override the method and may pick one with X.super.m(). Java avoids the C++ diamond problem because interfaces cannot hold instance fields, so there is never duplicated state to merge, only behaviour, and the rules above settle behaviour at compile time.

Why this question is asked

When Java 8 gave interfaces method bodies, it also gave Java a limited form of multiple inheritance, and with it the old diamond question. Interviewers ask about the three rules because they are short, precise and easy to half-remember, and because the comparison with C++ shows whether we understand what really makes multiple inheritance hard. In this answer, we will recap what the diamond problem is, walk through each of Java's three rules with code, see a fourth detail that often surprises people, abstract re-declaration, and finish with why the design avoids the C++ problems.

1A Quick Recap of the Diamond Problem

The diamond problem gets its name from the shape of the inheritance graph. A class D inherits from B and C, and both B and C inherit from A. If A has a method that B and C each override, which one does D get? And if A has a field, does D contain one copy of it or two?

In C++, which allows a class to inherit from several classes, both questions are real. A plain diamond gives D two separate copies of A's fields, and a call to the shared method is ambiguous until the programmer qualifies it. C++ offers virtual inheritance to share a single copy of A, at the cost of extra complexity in construction and layout. Java's designers wanted the useful part, sharing behaviour, without the state part.

2Rule 1: The Class Always Wins

If the class itself, or any of its superclasses, declares the method, that declaration wins over any default method from any interface. This holds even if the superclass method is abstract.

Java
interface Greeter { default String greet() { return "interface"; } }
class Base { public String greet() { return "class"; } }
class Child extends Base implements Greeter { }
System.out.println(new Child().greet()); // class
abstract class AbstractBase { public abstract String greet(); }
class Concrete extends AbstractBase implements Greeter { }
// error: Concrete is not abstract and does not override abstract method greet()

Notice the second case. Greeter offers a body, but the abstract method from the class hierarchy still takes priority, so Concrete must implement greet() itself. The reason for this rule is compatibility: when a library adds a default method to an interface, every existing class that already had that method keeps behaving exactly as before.

3Rule 2: The Most Specific Interface Wins

If no class in the hierarchy provides the method, Java looks at the interfaces. If one interface is a subinterface of another and overrides the default, the subinterface is more specific and its version wins.

Java
interface Animal { default String sound() { return "..."; } }
interface Dog extends Animal { @Override default String sound() { return "woof"; } }
class Puppy implements Animal, Dog { }
System.out.println(new Puppy().sound()); // woof

The logic is that Dog already knew about Animal's version and replaced it deliberately, so there is nothing to decide. This is also what keeps the true diamond calm in Java: if Dog and Cat both extend Animal and only Dog overrides sound(), a class implementing Dog and Cat gets Dog's version, because Cat merely inherited Animal's and Dog's is more specific than Animal's.

4Rule 3: Otherwise, the Class Must Choose

If two unrelated interfaces both provide a default with the same signature, neither rule above gives a single answer, and the class does not compile until it overrides the method. Inside the override it may delegate to one of them with X.super.m().

Java
interface Walker { default String move() { return "walk"; } }
interface Swimmer { default String move() { return "swim"; } }
class Duck implements Walker, Swimmer {
@Override public String move() {
return Walker.super.move() + " and " + Swimmer.super.move();
}
}
System.out.println(new Duck().move()); // walk and swim
Which move() does the class get?
SituationResultRule
Class or superclass declares the methodThat method, even if abstract1: class wins
Subinterface overrides parent's defaultThe subinterface's default2: most specific wins
Two unrelated defaultsCompile error until the class overrides3: class must choose
Subinterface re-declares it abstractImplementers must provide it2 applied to an abstract method
Java tries rule 1, then rule 2, then asks the programmer.

So the ambiguity that C++ reports at the call site is reported in Java at the class that creates it, once, and resolved there explicitly. Callers of Duck never see it.

5Abstract Re-declaration: Taking a Default Away

A subinterface can also cancel a default by declaring the same method again without a body. Under rule 2 the subinterface is more specific, and what it says is "this method is abstract", so its implementers must write the method themselves.

Java
interface Strict extends Animal {
@Override String sound(); // abstract again: Animal's default is hidden
}
class Robot implements Strict { }
// error: Robot is not abstract and does not override abstract method sound() in Strict

This is useful when a parent's default is a reasonable guess for most types but dangerous for a particular family. The subinterface can force every implementer in that family to think about it, without touching the parent.

6Why Java Avoids the C++ Diamond Problem

The hard part of the C++ diamond is state. Two copies of a base class's fields means two copies of its invariants, and keeping them consistent, or sharing one copy through virtual inheritance, affects construction order and object layout. Java interfaces cannot declare instance fields; their fields are always public static final constants. A class still has exactly one superclass, so it has exactly one chain of instance state and one chain of constructors.

What remains is behaviour, and behaviour conflicts are much easier to handle: the three rules settle every case at compile time, and the only ambiguous case produces an error with a clear fix. So Java has multiple inheritance of type and of behaviour, but single inheritance of state, and that is the combination that avoids the classic problems.

7Follow-up questions

Can a default method override a method from Object, such as toString?

No. An interface may not declare a default method that overrides equals, hashCode or toString from Object, and javac rejects it. Because of rule 1 such a default could never be used anyway: every class inherits those methods from Object, and the class always wins.

Do the rules depend on the order of the implements clause?

No. Listing Walker before Swimmer or the other way round makes no difference. That is deliberate, because behaviour that depended on the order of a list would change when someone tidied the code.

Is rule 1 applied even when the superclass method is less visible?

The superclass method still takes priority, but an interface method is public, so if the inherited class method is not public the compiler reports an error that the class is trying to assign weaker access. The fix is to make the class method public or override it publicly.

8Conclusion

In this answer, we saw Java's three rules for choosing an inherited method: the class or superclass wins, then the most specific interface wins, and otherwise the class must override and may delegate with X.super.m(). We saw that a subinterface can re-declare a method as abstract to remove a default for its implementers. And we saw that Java avoids the C++ diamond problem because interfaces carry no instance state, so only behaviour can conflict, and the compiler settles that.

What to say in the interviewAbout 70 seconds aloud

Open with

Java uses three rules, in order. First, the class wins: if the class or any superclass declares the method, that is what you get, even if the superclass method is abstract, which was essential so that adding defaults to old interfaces never changed existing classes.

Then, if they want more

Second, if only interfaces provide it, the most specific interface wins, so a subinterface that overrides its parent's default beats the parent. Third, if two unrelated interfaces both provide a default, the class does not compile until it overrides the method, and it can delegate with something like Walker.super.move().

A related detail is that a subinterface can re-declare a method abstract, which removes the parent's default for everyone implementing the subinterface. As for C++, the real pain of the diamond there is state: you can end up with two copies of the base class's fields, or you need virtual inheritance.

Java interfaces cannot have instance fields, and a class still has one superclass, so there is only one chain of state and constructors. Only behaviour can conflict, and the three rules resolve that at compile time.

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 "Explain the three rules Java uses to resolve which method is inherited when classes and...": https://scrivio-interview-library.netlify.app/java/interfaces-and-abstract-classes/27d220a987/