Clean • Professional
Modern Java (Java 14–17+) introduced Records and Sealed Classes to help developers write cleaner, safer, and more maintainable code. These features reduce boilerplate and provide better control over data and class design.
They are widely used in real-world applications such as APIs, domain modeling, and system design.
Records are perfect for creating simple data-carrying classes (DTOs) without writing boilerplate code.
record User(String name, int age) {}
public class Main {
public static void main(String[] args) {
User u = new User("Amit", 25);
System.out.println(u.name());
System.out.println(u.age());
System.out.println(u);
}
}
Output:
Amit
25
User[name=Amit, age=25]
What this shows:
name(), age())equals(), and hashCode()Records also support validation using a compact constructor, which allows you to enforce rules when an object is created.
record Product(String name, double price) {
public Product {
if (price < 0) {
throw new IllegalArgumentException("Price cannot be negative");
}
}
}
public class Main {
public static void main(String[] args) {
Product p = new Product("Laptop", 50000);
System.out.println(p);
}
}
Use case:
Records can also contain custom methods, allowing you to add simple business logic while still keeping the class clean and concise.
record Employee(String name, double salary) {
public double yearlySalary() {
return salary * 12;
}
}
public class Main {
public static void main(String[] args) {
Employee e = new Employee("Rahul", 30000);
System.out.println(e.yearlySalary());
}
}
Use case:
Records can also store collections like List, making them useful for grouping multiple values together in a clean and concise way.
import java.util.List;
record Order(int id, List<String> items) {}
public class Main {
public static void main(String[] args) {
Order o = new Order(1, List.of("Book", "Pen"));
System.out.println(o.items());
}
}
Important Note:
List can still be mutable depending on implementationRecords are widely used in real-world applications like APIs to represent structured response data in a clean and concise way.
record ApiResponse(String status, String message, int code) {}
public class Main {
public static void main(String[] args) {
ApiResponse res = new ApiResponse("SUCCESS", "Data fetched", 200);
System.out.println(res);
}
}
Use case:

Sealed classes allow you to restrict which classes can extend a parent class, ensuring a controlled and predictable hierarchy.
sealed class Shape permits Circle, Rectangle {
}
final class Circle extends Shape {
}
final class Rectangle extends Shape {
}
What this does:
Circle and Rectangle can extend ShapeShapeA non-sealed subclass removes the restriction of a sealed class and allows further inheritance freely.
sealed class Animal permits Dog, Cat {
}
final class Dog extends Animal {
}
non-sealed class Cat extends Animal {
}
class PersianCat extends Cat {
}
Explanation:
Dog is final, so it cannot be extended furtherCat is non-sealed, so it is open for inheritancePersianCat can extend Cat without any restrictionSealed interfaces allow you to control which classes can implement an interface, ensuring a fixed and well-defined set of implementations.
sealed interface Vehicle permits Car, Bike {
}
final class Car implements Vehicle {
}
final class Bike implements Vehicle {
}
Use case:
Sealed classes are very useful in real-world systems like payments, where you want to control allowed types while keeping some flexibility.
sealed class Payment permits UPI, Card, Cash {
}
final class UPI extends Payment {
}
final class Card extends Payment {
}
non-sealed class Cash extends Payment {
}
Use case:

This example combines Sealed Interfaces, Records, and Pattern Matching to build clean, modern, and expressive Java code.
sealed interface Shape permits Circle, Rectangle {}
record Circle(double radius) implements Shape {}
record Rectangle(double length, double width) implements Shape {}
public class Main {
static double area(Shape shape) {
return switch (shape) {
case Circle c -> Math.PI * c.radius() * c.radius();
case Rectangle r -> r.length() * r.width();
};
}
public static void main(String[] args) {
System.out.println(area(new Circle(5)));
System.out.println(area(new Rectangle(4, 6)));
}
}
What this demonstrates:
Circle and Rectangle are allowed
Modern Java features like Records and Sealed Classes help developers write more structured, maintainable, and expressive code.
Records
Sealed Classes
Records and Sealed Classes are powerful features in modern Java that improve both code quality and design clarity.
They reduce unnecessary complexity while giving developers better control over data and class structure.
What Developers Gain
👉 Overall, these features are essential for modern Java development, and are highly valuable for interviews, enterprise applications, and real-world projects.