Interfaces can hold private helper methods to share code between default methods.
interface Calc {
default int doubled(int n) { return scale(n, 2); }
default int tripled(int n) { return scale(n, 3); }
private int scale(int n, int by) { return n * by; }
}
Calc c = new Calc() {};
System.out.println(c.doubled(5) + " " + c.tripled(5));
10 15
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-26