March 2019
Intermediate to advanced
242 pages
6h 21m
English
Suppose that you want to limit the values that can be passed to a field in your data class during its instantiation. This is feasible; just override the default constructor. The following is an example:
record Emp(String name, int age) {
// override default constructor
@Override
public Emp(String name, int age) {
// validate age
if (age > 70)
throw new IllegalArgumentException("Not employable above 70 years");
else {
// call default constructor
default.this(name, age);
}
}
}
Similarly, you can override the default implementations of object methods, such as equals(), hashCode(), and toString(), and other methods, such as the accessor methods.
Overriding the default behaviors of the methods of your data class ...
Read now
Unlock full access