The implements Keyword: Class Adoption of Interfaces
What you'll learn: How a class formally commits to fulfilling an interface contract using the implements keyword.
Making the Promise Official
You've learned that an interface declares a contract—a set of abstract methods that define behavior. But how does a class actually adopt that contract? The answer is the implements keyword.
When you write class MyClass implements MyInterface, you're making a binding promise: "This class will provide concrete implementations for every abstract method declared in this interface." If you forget even one method, the compiler refuses to let your class be instantiated—you must either implement all methods or declare the class abstract.
Basic Syntax
interface Playable {
void play();
void pause();
}
class MusicPlayer implements Playable {
@Override
public void play() {
System.out.println("Music playing...");
}
@Override
public void pause() {
System.out.println("Music paused.");
}
}
Notice three things:
- The
implements Playabledeclaration comes after the class name - Every method from
Playableis implemented inMusicPlayer - The
@Overrideannotation confirms you're fulfilling the contract
The All-or-Nothing Rule
You cannot partially implement an interface (unless the class itself is abstract). This is the core guarantee: once a class implements an interface, you know those methods exist.
class BrokenPlayer implements Playable {
public void play() { /* implemented */ }
// Missing pause() — compiler error!
}
Key Takeaway: The implements keyword creates a binding contract between a class and an interface, requiring the class to provide concrete implementations for all abstract methods or be declared abstract itself.