← All topics

Java 21 OCP · Objective: Records and Sealed classes

Records & Sealed Classes

7 Enthuware questions, 7 misses. Below are the rules behind each one, then original drills written to hit the same rules from new angles. Every code sample was compiled and run on JDK 21.

Rules on one screen

Records
Record patterns (Java 21)
Sealed types

The 7 Enthuware misses, in one line each

Q1 · Record body

An extra int rank; in a record body

A record body can't declare instance fields. The only fix among the options was making it static. Making it private or giving it a value doesn't help.

Hook: an extra field in a record must be static, and nothing else rescues it.
Q2 · Constructors

How many constructors can a record have?

Any number. There's exactly one canonical constructor (compact or normal form) plus any number of overloads that start with this(…).

Hook: compact and normal canonical are the same constructor written two ways.
Q3 · Inheritance

Extends vs implements

A record can't use extends and can't be extended, but it can implement any number of interfaces. It can hold other records, and it can be Serializable (deserialization goes through the canonical constructor).

Hook: the only kind of inheritance a record has is sideways, through interfaces.
Q4 · Sealed + record methods + patterns

Every marked line looked odd, and none was invalid

A sealed interface permitting a record, a redundant final, a stray ;, a method with a parameter, a method called setRole that assigns nothing, and nested var patterns: all legal. The generated accessor role() implements the interface's String role().

Hook: "immutable" means you can't assign to a field. A record can have any methods.
Q5 · Generic record patterns

Relation(var a, var b) on a Relation<Student, Course>

Type arguments are inferred, so you don't need <> and it isn't allowed. a.name fails because the field is private (use a.name()). Pattern variables are out of scope after ||.

Hook: no diamond in patterns. Use accessors, not fields. After ||, the pattern variables are gone.
Q6 · Sealed

A sealed class can't be final

Concrete and nested classes can be sealed. Records and enums can't, but both can implement a sealed interface.

Hook: final means no subclasses, sealed means only the listed ones, so you can't have both.
Q7 · Compact constructor

Compact Journal { id = id + 1; } plus a normal Journal(int id, String name)

That's a duplicate canonical constructor. Removing the normal one fixes it, and the compact constructor's id = id + 1 changes the parameter, which then becomes the field's value.

Hook: in a compact constructor you never write this.x. In a normal one, you must write every this.x.

My drill progress on this page

Work through blocks 1–7, then sit the timed mock. Reload the page to reshuffle. Aim for full marks on each block before you try the mock.

Drill blockQsLastBestRunsLast done
Loading…

1 · Record body Q1

2 · Constructors Q2 · Q7

3 · Inheritance & modifiers Q3

4 · Accessors & object methods Q4

5 · Record patterns Q4 · Q5

6 · Sealed types Q4 · Q6

7 · True or false (quick-fire)

Each statement on its own. This is the "give each option a yes or no" habit.

Timed mock

25 questions drawn at random from all the blocks above, with fresh question and option order every time. There's no feedback until you submit. You get 60 minutes, roughly the real exam's pace, and the pass mark is 68%.

← All topics