Java Foundations· 30 min

Modern Java Language Features

Review enums, records, sealed classes, nested classes, pattern matching, and how to choose language features deliberately.

Java interview lesson
Question 3 · Not reviewed

Foundations · modern Java

Can you explain this clearly in an interview?

Use the strong answer as your reference, explain it aloud in your own words, then choose how soon you want to review it again.

Interview prompt

Modern Java Language Features

Explain it aloud in your own words

Strong answer

Modern Java features reduce ceremony while preserving explicit types. Records model transparent data carriers, sealed types restrict an inheritance hierarchy, and enums model a finite set of domain values. Use them when the domain is genuinely closed and the simpler representation improves correctness.

Key points

  • Records are shallowly immutable data carriers, not automatically deeply immutable objects.
  • Sealed hierarchies make exhaustive reasoning possible.
  • A record still needs validation when its invariants are stronger than field types.
  • New syntax should clarify a model, not hide important behavior.

Interviewer looks for

Mention the trade-off: concise syntax is valuable, but domain behavior and mutability still need deliberate design.

Java example

1record UserId(long value) {
2  UserId {
3    if (value <= 0) throw new IllegalArgumentException("positive id required");
4  }
5}

Topic interview questions

Revise this topic like an interviewer

These questions are tied to this lesson. Read each question aloud, then compare your explanation with the strong answer.

5 questions
Q1Basic

What problem does a record solve?

Strong answer

A record concisely models a transparent data carrier with final components, accessors, equals, hashCode, and toString generated from those components. It is shallowly immutable, so referenced objects can still be mutable.

Q2Intermediate

What are sealed classes useful for?

Strong answer

A sealed class or interface restricts which types may extend or implement it. This makes a domain hierarchy explicit and allows exhaustive reasoning in pattern matching and switch expressions.

Q3Intermediate

Are records always deeply immutable?

Strong answer

No. A record prevents reassignment of its component references, but a component such as a List can still be mutated unless the constructor makes a defensive copy or uses an immutable representation.

Q4Basic

When is an enum better than a set of constants?

Strong answer

An enum models a closed set of values with type safety and can hold fields and behavior. It prevents unrelated integers or strings from being passed accidentally as domain values.

Q5Intermediate

What is the trade-off when using var?

Strong answer

var removes repeated local variable type syntax while retaining static typing and compile-time checking. It should improve readability when the initializer makes the type obvious, not hide an important API or domain type.

How well do you know this?

Your review choice is saved locally and synced to your learner profile when signed in.