Interview Q&A · Java · 4 questions in this topic

Interfaces: Purpose and Modern Features — Java Interview Q&A

All Java topics

What interfaces guarantee, how they enable polymorphism, and what default, static and private methods added in modern Java. Part of Interfaces, Enums & Features · 294 questions in the Java bank.

Can interfaces declare static methods?

Answer

Yes. Since Java 8 an interface may declare static methods with bodies, alongside default methods that carry an implementation inherited by implementors. The older rule that interfaces permit only abstract instance methods is therefore outdated. Fields remain implicitly public, static and final, methods remain implicitly public, and since Java 9 interfaces may also declare private methods to share code between default methods.

Key points

  1. 1Static interface methods are invoked through the interface name and are not inherited by implementing classes.
  2. 2Default methods are inherited and may be overridden by any implementing class.
  3. 3Private interface methods support code reuse inside the interface and are invisible to implementors.

Example

Example.javajava
interface Greeter {
    static String hello() {
        return format();
    }
    default String hi(String name) {
        return format() + name;
    }
    private static String format() {
        return "Hi ";
    }
}

💡 Interviewers often repeat the outdated claim that interfaces forbid static methods: answer yes since Java 8 and contrast static dispatch with default dispatch.

What is an interface?

Answer

An interface is a reference type that specifies a capability as a set of method signatures and constants, without any instance state. It is implicitly abstract and cannot be instantiated on its own. A class opts into the contract with the implements keyword and must then supply every abstract method, unless the class itself is declared abstract.

Key points

  1. 1Interfaces define types: an interface reference can point at any object whose class implements it.
  2. 2A class may implement any number of interfaces, which gives Java multiple inheritance of type.
  3. 3Any field declared in an interface is a constant, implicitly public, static and final.

Example

Example.javajava
interface Drawable {
    void draw();
}
class Circle implements Drawable {
    public void draw() {
        System.out.println("Drawing a circle");
    }
}

What is the purpose of an interface?

Answer

An interface declares what an object can do while leaving how it does it to each implementation. That separation decouples callers from concrete classes, so code depends on the abstraction and any conforming implementation can be substituted. The payoff is polymorphism, easier testing with substitute implementations, and multiple inheritance of type when one class must play several roles.

Key points

  1. 1Decoupling: callers program against the interface, not the concrete class.
  2. 2Polymorphism: unrelated classes become interchangeable through a shared interface.
  3. 3Testability: a lightweight test double can stand in for a heavy real implementation.
  4. 4Multiple roles: one class can implement several interfaces at once.

💡 A strong one-line opener is that an interface defines WHAT without HOW, then expand into decoupling and testability.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits