Declaring an Interface: Syntax and Structure
What you'll learn: How to write an interface declaration in Java and understand the implicit modifiers on its methods.
What Is an Interface Declaration?
An interface declaration looks similar to a class declaration, but uses the interface keyword instead of class. It defines a contract—a set of method signatures that implementing classes must provide. Unlike abstract classes, interfaces traditionally don't contain method implementations (just the signatures).
Basic Syntax
Here's the structure for declaring an interface:
public interface Drawable {
void draw();
void resize(int width, int height);
double getArea();
}
Notice:
- Use
interfacekeyword instead ofclass - Methods have no body—just a semicolon
- No need to write
publicorabstractbefore methods
Implicit Modifiers
All interface methods are implicitly public and abstract. Java adds these modifiers automatically, even if you don't write them:
public interface Vehicle {
// These two declarations are identical:
void start();
public abstract void start(); // redundant but legal
}
Both versions mean exactly the same thing. The first is preferred—cleaner and less verbose.
Naming Conventions
Interfaces typically use adjectives ending in "-able" (like Runnable, Comparable) or nouns describing capabilities (like List, Vehicle). Use clear, descriptive names that communicate the contract.
Think of It As...
An interface is like a job description. It lists required skills (methods) without explaining how to perform them. Any class that "applies for the job" (implements the interface) must provide the actual implementation.
Key Takeaway: Interface declarations use the interface keyword and contain method signatures without bodies; all methods are implicitly public and abstract, so you don't need to write those modifiers explicitly.