Java - Records and Sealed Classes in Java (Modern Java Features)

Java has evolved significantly in recent versions to reduce boilerplate code and improve type safety. Two important modern features introduced are Records and Sealed Classes. Both are designed to make code more readable, maintainable, and secure, but they solve different problems.


1. Records in Java

What is a Record?

A Record is a special type of class introduced to represent pure data carriers. It is mainly used when you want to store data without writing repetitive code like constructors, getters, equals(), hashCode(), and toString().

Traditionally, Java required a lot of boilerplate code for simple data-holding classes. Records eliminate that.


Why Records were introduced

In earlier Java versions, a simple data class required:

  • Private fields

  • Constructor

  • Getter methods

  • equals() and hashCode() methods

  • toString() method

Records automatically generate all of these.


Syntax of Record

record Student(String name, int age) { }

This single line automatically creates:

  • Constructor: Student(String name, int age)

  • Getters: name() and age()

  • equals() and hashCode() methods

  • toString() method


Example Usage

Student s = new Student("Anita", 20);

System.out.println(s.name());
System.out.println(s.age());
System.out.println(s);

Key Characteristics of Records

  1. Immutable by default
    Fields cannot be changed after object creation.

  2. Compact syntax
    Reduces large boilerplate code.

  3. Final nature
    Records cannot extend other classes because they implicitly extend java.lang.Record.

  4. Shallowly transparent data carriers
    They are meant only for storing data, not behavior-heavy logic.


When to use Records

  • Data Transfer Objects (DTOs)

  • API responses

  • Configuration holders

  • Simple model classes


2. Sealed Classes in Java

What is a Sealed Class?

A Sealed Class is a class that restricts which other classes can extend or implement it.

It provides controlled inheritance, meaning only permitted classes can be its subclasses.


Why Sealed Classes were introduced

In traditional Java:

  • Any class can extend another class unless it is final

  • This can lead to uncontrolled inheritance

Sealed classes solve this by allowing the developer to explicitly define permitted subclasses.


Syntax of Sealed Class

sealed class Vehicle permits Car, Bike {
}

Only Car and Bike are allowed to extend Vehicle.


Subclass rules

Each permitted subclass must declare one of the following:

  1. final – cannot be extended further

  2. sealed – can restrict its own subclasses

  3. non-sealed – open for extension


Example

Sealed Parent Class

sealed class Shape permits Circle, Square {
}

Final Subclass

final class Circle extends Shape {
}

Non-sealed Subclass

non-sealed class Square extends Shape {
}

Key Characteristics of Sealed Classes

  1. Controlled inheritance
    Only specified classes can extend them.

  2. Improved security and design clarity
    Prevents unintended subclassing.

  3. Works well with pattern matching
    Especially in modern Java switch expressions.

  4. Enables better compile-time checks
    Compiler knows all possible subclasses.


3. Relationship Between Records and Sealed Classes

These two features are often used together in modern Java design:

  • Records → represent fixed data structures

  • Sealed Classes → define controlled type hierarchies

Example:
You might use a sealed class for different types of payments and records for each payment type’s data.


Summary

  • Records simplify data-holding classes by removing boilerplate code and making objects immutable by default.

  • Sealed Classes give strict control over inheritance and improve type safety.

  • Both features are part of modern Java’s effort to make code cleaner, safer, and more expressive.