Sub topic 7
Implementing an Interface
An interface lists methods that a type promises to have. An interface does nothing by itself. A class has to do the work. This chapter shows how a class keeps an interface's promises.
The `implements` Keyword
To implement an interface means to write a class that provides real code for every method the interface declares. You write the keyword implements after the class name, followed by the interface name.
Here is a small interface called Greeter. It has one method with no body.
interface Greeter {
String greet(String name);
}Any class that implements Greeter must supply a greet method that has the same name, the same parameter types, and the same return type.
A Worked Example
Two classes can keep the same promise in different ways. EnglishGreeter and SpanishGreeter both implement Greeter.
interface Greeter {
String greet(String name);
}
class EnglishGreeter implements Greeter {
@Override
public String greet(String name) {
return "Hello, " + name + "!";
}
}
class SpanishGreeter implements Greeter {
@Override
public String greet(String name) {
return "Hola, " + name + "!";
}
}
public class Main {
public static void main(String[] args) {
EnglishGreeter en = new EnglishGreeter();
SpanishGreeter es = new SpanishGreeter();
System.out.println(en.greet("Ana")); // prints: Hello, Ana!
System.out.println(es.greet("Ana")); // prints: Hola, Ana!
}
}The @Override annotation is a label that asks the compiler to check that this method really matches one from the interface. If you misspell the name, for example greeet, the compiler reports an error instead of quietly creating a new method.
Rules You Must Follow
| Rule | What happens if you break it |
|---|---|
| Implement every abstract method | Compile error: the class "is not abstract and does not override" the method |
| Declare the method `public` | Compile error: "attempting to assign weaker access privileges" |
| Match the parameters and return type exactly | The compiler treats it as a different method, and `@Override` flags the mistake |
Keeping a Promise Partly
Sometimes a class cannot supply every method yet. In that case it can be declared abstract. An abstract class may implement an interface and leave some methods for its subclasses to finish. Only a concrete class, meaning a class you can create with new, has to fill in everything.
interface Greeter {
String greet(String name);
}
abstract class PoliteGreeter implements Greeter {
// greet is still missing, and that is allowed here
}
class FormalGreeter extends PoliteGreeter {
@Override
public String greet(String name) {
return "Good day, " + name + ".";
}
}
public class Main {
public static void main(String[] args) {
System.out.println(new FormalGreeter().greet("Ana")); // prints: Good day, Ana.
}
}Exercise
This class does not compile: class FrenchGreeter implements Greeter { String greet(String name) { return "Bonjour, " + name; } }. Why, and how do you fix it?
Show answer
The method is missing public. Interface methods are public, so the implementing method must also be public. Change it to public String greet(String name) and add @Override above it.
Exercise
A class says implements Greeter but has no greet method. What two different ways can you make it compile?
Show answer
Add a public greet(String name) method that returns a String, or declare the class abstract so that a subclass has to provide greet later.