Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Write an interface with a default method, a static factory method and a private helper, and explain the rules for each.

In interface Greeter { String name(); default String greet() { return prefix() + name(); } static Greeter of(String n) { return () -> n; } private String prefix() { return "Hello, "; } }, the default method greet() gives implementers an implementation they inherit and may override; the static factory of() is called on the interface as Greeter.of and is not inherited by implementing classes; and the private method prefix(), allowed since Java 9, shares code between default methods without being visible to implementers. A default method may never override equals, hashCode or toString from Object.

Why this question is asked

Interfaces gained three kinds of method bodies after Java 8: default methods, static methods and, in Java 9, private methods. Most of us use them daily, through Comparator.comparing, List.of or Collection.stream, but the rules for each are different and easy to mix up under interview pressure. This coding question asks us to put all three into one small interface and explain why each rule exists. In this answer, we will write the Greeter interface, run it, and then go through default, static and private methods one at a time, including the conflicts that defaults can create.

1A Quick Recap of Interface Methods

Before Java 8, an interface could only declare abstract methods, which have no body and must be implemented by every class, plus constants. Java 8 added default methods, which have a body that implementing classes inherit, and static methods, which belong to the interface itself. Java 9 added private methods, which are helpers visible only inside the interface. All of these are still compatible with lambdas: an interface with exactly one abstract method is a functional interface, however many default, static and private methods it has.

2Writing the Greeter Interface

Here is the interface from the question, laid out over several lines, with a class that implements it and a lambda created by the factory.

Java
interface Greeter {
String name(); // abstract: implementers must provide it
default String greet() { // inherited, may be overridden
return prefix() + name();
}
static Greeter of(String n) { // called as Greeter.of(...)
return () -> n; // a lambda implements name()
}
private String prefix() { // Java 9+: a helper for defaults
return "Hello, ";
}
}
class FormalGreeter implements Greeter {
public String name() { return "Dr. Rao"; }
@Override public String greet() { return "Good evening, " + name(); }
}
System.out.println(Greeter.of("Ana").greet()); // Hello, Ana
System.out.println(new FormalGreeter().greet()); // Good evening, Dr. Rao

Notice that Greeter.of can return a lambda because Greeter has only one abstract method, name(). The lambda supplies name(), and greet() comes for free from the default method. FormalGreeter shows the other option: it overrides the default with its own version.

3Default Methods: Inherited and Overridable

A default method is an ordinary instance method with a body, declared in an interface with the default keyword. Every implementing class inherits it unless it overrides it. Inside the body, this refers to the implementing object, so a default can call the interface's abstract methods, which is how greet() uses name(). What a default cannot do is use instance fields, because interfaces have none.

When a class inherits methods with the same signature from several places, three rules decide which one wins. A method declared in the class or its superclasses wins over any interface default. A default in a more specific interface wins over one in an interface it extends. If two unrelated interfaces supply the same default, the class must override it and may call one of them explicitly.

Java
interface Polite { default String greet() { return "Please, hello"; } }
class Both implements Greeter, Polite { // without the override: compile error,
public String name() { return "Li"; } // Both inherits unrelated defaults for greet()
@Override public String greet() {
return Greeter.super.greet(); // chooses Greeter's version explicitly
}
}
System.out.println(new Both().greet()); // Hello, Li
Step throughWhich greet() does a class get?
  1. Does the class or a superclass declare greet()?
    YesThat method winsNoLook at the interfaces
  2. Is one interface's default more specific?
    YesThe most specific default winsNoCheck for a conflict
  3. Do two unrelated interfaces supply it?
    YesCompile error until the class overrides itNoThe single default is inherited

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

Class wins, then the most specific interface, otherwise the class must resolve the conflict.

4Static Methods: Called on the Interface, Never Inherited

A static interface method belongs to the interface. We call it as Greeter.of("Ana"), and that is the only way. Unlike static methods in classes, it is not inherited by implementing classes and cannot be called through an instance.

Java
Greeter g = Greeter.of("Ana"); // fine
FormalGreeter.of("Ana"); // compile error: of() is not a member of FormalGreeter
g.of("Bo"); // compile error: a static interface method must be called on the interface

The reason for this stricter rule is that a class can implement many interfaces. If static methods were inherited, two interfaces with a static of() would create ambiguity in every class that implements both. Keeping them attached to the interface avoids the problem and makes the call site say exactly which factory runs. This is why the JDK can offer List.of, Set.of and Map.of without conflicts.

5Private Methods: Shared Helpers Hidden From Implementers

Before Java 9, if two default methods needed the same helper code, the helper had to be another default method, which made it part of the public API and something implementers could override. Private interface methods solve that. prefix() can be used by greet() and by any other default method, but implementing classes cannot see or override it, and callers cannot call it.

There are two forms. A private instance method, like prefix(), can be called from default methods and other private instance methods. A private static method can be called from anywhere in the interface, including static methods such as of(). A static method cannot call prefix(), because prefix() needs an instance.

6Why a Default Cannot Override equals, hashCode or toString

If we try to add a default equals or toString to Greeter, the compiler rejects it.

Java
interface Greeter {
String name();
default String toString() { return "Greeter " + name(); } // compile error:
// default method toString in interface Greeter overrides a member of java.lang.Object
}

The reason follows from the 'class wins' rule. Every object inherits toString from the Object class, and a class method always beats an interface default, so such a default could never run. Rather than allow dead code that looks like it works, Java forbids it. Behaviour of that kind belongs in a class, for example an abstract skeletal class that implements the interface.

7Follow-up questions

Can a default method be declared final or synchronized?

No. Neither modifier is allowed on interface methods. A default method is meant to be overridable, so final would defeat its purpose, and synchronized would lock on the implementing object in a way the interface author cannot control. Implementers can still make their own override synchronized.

Is a default method a good place for core business logic?

Usually not. Defaults were designed for evolving interfaces and small conveniences built on the abstract methods. Heavy logic in defaults spreads behaviour across interfaces with conflict rules readers must know, and it cannot use state, so it tends to end up awkward. Larger behaviour fits better in classes.

What does Greeter.super.greet() mean exactly?

It calls the default greet() from the Greeter interface on the current object, bypassing the class's own override. It may only name a direct superinterface of the class, and it is the standard way to resolve a conflict between two defaults or to extend a default rather than replace it.

8Conclusion

In this answer, we wrote an interface with all three kinds of method body. The default greet() is inherited and overridable, with clear rules when several defaults meet; the static of() is called only as Greeter.of and is never inherited, which avoids conflicts across interfaces; and the private prefix() shares code between defaults without being visible to implementers. We also saw that a default may not override equals, hashCode or toString, because a class method from Object would always win.

What to say in the interviewAbout 90 seconds aloud

Open with

I'd write the interface with name as its only abstract method, a default greet that returns prefix plus name, a static of that returns a lambda, and a private prefix that returns Hello.

Then, if they want more

Because there's only one abstract method, of can return a lambda that supplies name, so Greeter.of Ana dot greet prints Hello, Ana. Then the rules. A default method is an instance method with a body that implementing classes inherit and can override; it can call the abstract methods but has no fields to use.

If a class gets the same default from two unrelated interfaces, it has to override it, and it can call one with Greeter.super.greet. Class methods always win over defaults.

A static interface method belongs to the interface: you call it as Greeter.of, and unlike class statics it isn't inherited by implementers and can't be called through an instance, which avoids ambiguity when a class implements several interfaces. That's why List.of and Set.of can coexist.

A private method, since Java 9, is a helper shared by default methods; implementers can't see or override it, and a private static one can be used from static methods. Finally, a default method can't override equals, hashCode or toString. The compiler rejects it, because Object's versions would always win under the class-wins rule, so the default could never run.

About this answer

Last updated
12 November 2026
Versions it names
Java 9, 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 "Write an interface with a default method, a static factory method and a private helper...": https://scrivio-interview-library.netlify.app/java/interfaces-and-abstract-classes/76a326471b/