Is there any way of using records with inheritance?
Records already extend java.lang.Record
. As Java does not allow multiple inheritance, records cannot extend any other class.
Consider, for example, the following record Point
:
public record Point(double x, double y) {}
You can compile it using:
javac --enable-preview -source 14 Point.java
With the help of javap
, you can can have details about the code generate for Point
:
javap -p Point
The output will be:
Compiled from "Point.java"
public final class Point extends java.lang.Record {
private final double x;
private final double y;
public Point(double, double);
public java.lang.String toString();
public final int hashCode();
public final boolean equals(java.lang.Object);
public double x();
public double y();
}