As records are final by default and can extend interfaces. We can define sealed interfaces and let record implement them for better code management.
Example
Consider the following example −
Java15Tester.java
public class Java15Tester {
public static void main(String[] args) {
Person employee = new Employee(25, "Robert");
System.out.println(employee.id());
System.out.println(employee.name());
}
}
sealed interface Person permits Employee, Manager {
int id();
String name();
}
record Employee(int id, String name) implements Person {}
record Manager(int id, String name) implements Person {}
Compile and Run the program
$javac -Xlint:preview --enable-preview -source 15 Java15Tester.java
$java --enable-preview Java15Tester
Output
25
Robert