Implementing interfaces

A data class can implement an interface and its abstract methods, or just inherit its default methods. The following is an example:

interface Organizer {} 
interface Speaker { 
   abstract void conferenceTalk(); 
} 
 
abstract record Emp(String name, int age); 
 
record Manager(String name, int age, String country)  
   extends Emp(name, age)                    // subclass a record
   implements Organizer;                     // implement one interface
 
record Programmer(String name, int age, String programmingLang)  
   extends Emp(name, age)                    // subclass a record
   implements Organizer, Speaker {           // implementing multiple 
// interfaces public void conferenceTalk() { // implement abstract
// method //.. code // from interface Speaker } };

The preceding code defines a tagging interface, Organizer (without any methods), and an interface, Speaker, with an abstract method, conferenceTalk(). We have two cases, as follows:

A data class can implement a single or multiple interfaces.