CKTIS / Java
implements — implementing interfaces
implements — implementing interfaces: An implements clause on a class or record declaration lists direct superinterfaces; a concrete class must provide inherited abstract methods.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
implementsAn implements clause on a class or record declaration lists direct superinterfaces; a concrete class must provide inherited abstract methods.
Use it so a type fulfills an interface contract and is substitutable through that interface without inheriting a class implementation.
Two inherited default methods with the same signature can require an explicit override; implements accepts neither a class nor a primitive.
An abstract class may leave a method unimplemented, and records and enums can also implement interfaces; erasure clashes are compile-time errors.
The User record implements Named; the interface reference prints Ada, and instanceof confirms the concrete type with true.
Examples
interface Named {
String name();
}
record User(String name) implements Named {}
public class Main {
public static void main(String[] args) {
Named named = new User("Ada");
System.out.println(named.name());
System.out.println(named instanceof User);
}
}
Result
Ada true
The User record implements Named; the interface reference prints Ada, and instanceof confirms the concrete type with true.