Interview Q&A · Java · 4 questions in this topic
Interfaces: Purpose and Modern Features — Java Interview Q&A
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
- 1Static interface methods are invoked through the interface name and are not inherited by implementing classes.
- 2Default methods are inherited and may be overridden by any implementing class.
- 3Private interface methods support code reuse inside the interface and are invisible to implementors.
Example
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
- 1Interfaces define types: an interface reference can point at any object whose class implements it.
- 2A class may implement any number of interfaces, which gives Java multiple inheritance of type.
- 3Any field declared in an interface is a constant, implicitly public, static and final.
Example
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
- 1Decoupling: callers program against the interface, not the concrete class.
- 2Polymorphism: unrelated classes become interchangeable through a shared interface.
- 3Testability: a lightweight test double can stand in for a heavy real implementation.
- 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.
What are the features of interfaces?
Answer
Methods without bodies are implicitly public and abstract, while fields are implicitly public, static and final constants. Since Java 8 interfaces may also carry behaviour through default and static methods, and since Java 9 they may use private methods for internal reuse. An interface holds no instance state and has no constructor, one interface may extend several others, a single-method interface can serve as a functional interface for lambdas, and since Java 17 an interface can be sealed to control which types implement it.
Key points
- 1No instantiation and no instance state: only constants plus method declarations and bodies.
- 2Default methods evolve published interfaces without breaking existing implementors.
- 3Static methods provide helpers namespaced on the interface itself.
- 4Functional interfaces plus lambdas turn single-method interfaces into lightweight behaviour parameters.
- 5Sealed interfaces restrict the permitted set of implementing types.
Example
interface Printer { int MAX_COPIES = 100; // public static final void print(String text); // public abstract default void printTwice(String text) { print(text); print(text); } }
Recommended resourcesAffiliate links · may earn us a commission