Interface
Интерфейс
An interface tells one part of a program how to use another: what operations it can call, what to pass in, and what to expect back. It lets the caller use those operations without knowing how they work internally.
What is an interface?
Think of an interface from the caller's side. If a report needs today's date, it should be able to ask a clock for it without knowing whether the clock reads system time or returns a fixed date for a test. The interface is what the caller can rely on; the implementation is what actually does the work.
An interface doesn't require an interface keyword. A module's exported functions are its public interface even if the language has no dedicated syntax for declaring one. An HTTP API is another kind of programming interface: its callers use requests and responses rather than method calls. A user interface, by contrast, is how a person interacts with software.
What does the language check?
In Java, an interface can declare methods that implementing classes must provide. A class declares that it implements the interface, and the compiler checks that a concrete class has the required methods. Go takes a different approach: a type satisfies an interface implicitly if its method set includes the required methods with matching signatures.
A type checker can verify that a method takes and returns the right types. It generally can't verify the promise behind them. For a method called today(), for instance, the return type doesn't tell you whether “today” means the date in UTC or in the user's time zone. Document that rule and test it across implementations.
Example: put the clock behind an interface
import java.time.LocalDate;
interface DateSource {
LocalDate today();
}
class Report {
String title(DateSource dates) {
return "Daily report: " + dates.today();
}
}
Production code can supply a DateSource that reads the system clock using the report's configured time zone. A test can supply one that always returns the same date, so the expected title doesn't change at midnight. Report doesn't choose the implementation; it receives it. That's dependency injection, and calling today() through the shared type is an example of polymorphism.
When is an interface worth adding?
An interface is useful when you want to keep business logic independent of something that changes for different reasons, such as a database client or an external service. Start with what the caller needs. If it only reads a date, it shouldn't depend on an interface that also sets the clock or manages time zones. On the other hand, giving every class a matching interface adds another type to maintain without necessarily making the code easier to change.
How it comes up in interviews
In a Java interview, you may be asked to choose between an interface and an abstract class. An interface is a good fit for a role that otherwise unrelated classes can play. An abstract class can supply shared instance state and initialize it in a constructor. Avoid saying that interfaces never contain implementation: Java interfaces can provide default methods.