91. Iterating what we cannot have in a record
There are several artifacts that we cannot have in a Java record. Let’s tackle the top 5 one by one.
A record cannot extend another class
Since a record already extends java.lang.Record
and Java doesn’t support multiple inheritances, we cannot write a record that extends another class:
public record MelonRecord(String type, float weight)
extends Cucurbitaceae {…}
This snippet doesn’t compile.
A record cannot be extended
Java records are final
classes, so they cannot be extended:
public class PumpkinClass extends MelonRecord {…}
This snippet doesn’t compile.
A record cannot be enriched with instance fields
When we declare a record, we also provide the components that become the instance fields of the record. Later, we cannot add more instance fields as we could in a typical class:
public record MelonRecord(String type, float weight) {
private String...