skip to content

How do you declare an interface in Java, and what can it contain?

level: juniorimportance: must knowfreq 75%

answer

  1. `interface` keyword, not `class`
  2. Method signatures, no body (classically)
  3. `implements` keyword on the class
  4. Constants are public static final
  5. Java 8 added default + static methods

basics

~20 s

You write interface Name { ... }. An interface lists method signatures (no bodies) that implementing classes must provide, and it can hold constants. A class uses implements Name to promise it provides those methods.

solid answer

~40 s

An interface is a reference type declared with the `interface` keyword instead of `class`. It defines a contract: a set of abstract method signatures (name, parameters, return type) with no implementation, plus constants. Classically it could only hold abstract methods and `public static final` constants; since Java 8 it can also hold `default` methods (with a body, inherited by implementors), `static` methods, and since Java 9 `private` helper methods. A class declares `class Foo implements Bar` and must supply a body for every abstract method (or be declared `abstract`). A type can implement many interfaces. Interfaces let you program to a contract rather than a concrete class, which is the foundation of polymorphism and loose coupling in Java.

code

java · 13 lines
java
public interface Animal {
    String makeSound();          // abstract method (implicitly public abstract)
    int LEGS = 4;                // constant (implicitly public static final)
}

public class Dog implements Animal {
    @Override
    public String makeSound() { return "Woof"; }
}

// Program to the interface, not the class:
Animal a = new Dog();
System.out.println(a.makeSound()); // Woof

go deeper

for a junior

Knows the interface/implements keywords, that abstract methods have no body, and that constants live there.

for a middle

Distinguishes abstract/default/static/private members and explains 'program to an interface' for decoupling.

for a senior

Articulates why interfaces enable polymorphism, DI and testability, and the evolution from Java 8 default methods.

for a principal

Frames interfaces as API design surface and contracts; weighs interface vs abstract class for library evolution and backward compatibility.

## What an interface is In Java, a **type** is the kind of a value (what operations it supports). A **class** is the most common type — it bundles data (fields) and behavior (methods) and can be instantiated with `new`. An **interface** is a different kind of type: a pure **contract** that says *what* operations a thing supports, without (traditionally) saying *how*. You declare one with the `interface` keyword: ```java public interface Animal { String makeSound(); // a method signature, no body } ``` A **method signature** is the method's name, its parameter list, and (for matching) is paired with a return type — but no `{ ... }` body. The body is supplied later by a class that *implements* the interface. ## Implementing an interface A class promises to fulfill the contract with the `implements` keyword and must then provide a real body for every abstract method: ```java public class Dog implements Animal { @Override public String makeSound() { return "Woof"; } } ``` If a class does **not** implement every abstract method, it must itself be declared `abstract` (meaning it cannot be instantiated and leaves the rest to subclasses); otherwise the code does not compile. ## What an interface can contain 1. **Abstract methods** — signatures without bodies. These are the core contract. 2. **Constants** — fields, which are implicitly `public static final` (a named, unchangeable, shared value). 3. **`default` methods** (Java 8+) — methods *with* a body, marked `default`, inherited by every implementor unless overridden. They let an interface evolve without breaking existing implementors. 4. **`static` methods** (Java 8+) — utility methods called on the interface itself, e.g. `Comparator.naturalOrder()`. 5. **`private` methods** (Java 9+) — helper bodies shared by `default`/`static` methods, not part of the public contract. 6. **Nested types** — interfaces, classes, enums declared inside. What an interface **cannot** have (classically): instance fields holding per-object state, constructors (you never `new` an interface), or method bodies (before Java 8). ## Why interfaces exist They enable **programming to an interface**: callers depend on the contract (`Animal`), not a concrete class (`Dog`). You can swap implementations freely. This underpins **polymorphism** (one reference type, many runtime behaviors), **loose coupling**, dependency injection, and testability (you can pass a fake implementation in tests). ## Interface vs class quick contrast - A class can `extends` **one** class but `implements` **many** interfaces — interfaces are Java's answer to multiple inheritance of *type*. - A class has state and identity; an interface is a behavioral contract.

  • Can you instantiate an interface directly with `new`?
    No. An interface has no constructor and no instance fields, so `new Animal()` is illegal. You instantiate a class that implements it (or use an anonymous class / lambda for a functional interface).
  • What happens if a concrete class doesn't implement all abstract methods of its interface?
    It won't compile unless the class is declared `abstract`. Abstract classes can leave methods unimplemented for subclasses to finish.

An interface is like a job description: it lists the duties a hire must perform (the methods) without saying who does them or how. Any candidate (class) who can do all the duties can fill the role.

  • Saying interfaces can never have method bodies (false since Java 8 default/static methods)
  • Confusing `implements` with `extends` (classes extend a class, implement interfaces)
  • Claiming you can instantiate an interface with `new`
  • Thinking interfaces hold per-instance mutable state

context