DEV Community

Rajesh Mishra
Rajesh Mishra

Posted on • Originally published at howtostartprogramming.in

Java 17 Sealed Classes Explained with Examples (2026)

Java 17 Sealed Classes Explained with Examples (2026)

Java 17 sealed classes explained with examples. Learn how to implement sealed classes in Java 17 and improve your code quality.

In Java, classes can be either concrete or abstract. However, there are cases where we want to restrict the instantiation of a class to only a specific set of subclasses. This is where sealed classes come into play. Sealed classes are a new feature introduced in Java 17 that allows us to define a class that can only be extended by a fixed set of subclasses. This feature helps to improve code quality by making it more explicit and self-documenting.

Before Java 17, we could achieve similar results using other design patterns, such as the "sealed" pattern using private constructors and static factory methods. However, this approach had its own set of limitations and was not as straightforward as using sealed classes. With the introduction of sealed classes, we can now define a class hierarchy that is more explicit and easier to maintain.

The problem that sealed classes solve is that of implicit class hierarchies. In Java, when we define an abstract class, it can be extended by any other class. This can lead to unexpected behavior and make the code harder to reason about. By using sealed classes, we can define a class hierarchy that is explicit and self-documenting, making it easier to understand and maintain.

WHAT YOU'LL LEARN

  • How to define a sealed class in Java 17
  • How to restrict the instantiation of a class to only a specific set of subclasses
  • The differences between sealed, final, and abstract classes
  • How to use sealed classes to improve code quality and make it more explicit
  • Common use cases for sealed classes, such as defining a class hierarchy for a specific domain model
  • How to handle errors and exceptions when working with sealed classes

A SHORT CODE SNIPPET

public sealed class Vehicle permits Car, Truck, Motorcycle {
public abstract void drive();
}

public final class Car extends Vehicle {
@Override
public void drive() {
System.out.println("Driving a car");
}
}

public final class Truck extends Vehicle {
@Override
public void drive() {
System.out.println("Driving a truck");
}
}

public final class Motorcycle extends Vehicle {
@Override
public void drive() {
System.out.println("Driving a motorcycle");
}
}
Enter fullscreen mode Exit fullscreen mode

KEY TAKEAWAYS

  • Sealed classes allow us to define a class hierarchy that is explicit and self-documenting
  • Sealed classes can only be extended by a fixed set of subclasses, which are specified using the permits keyword
  • Sealed classes can be used to improve code quality and make it more explicit
  • Sealed classes are particularly useful when defining a class hierarchy for a specific domain model

CTA

Read the complete guide with step-by-step examples, common mistakes, and production tips:
Java 17 Sealed Classes Explained with Examples (2026)
https://howtostartprogramming.in/java-17-sealed-classes-explained-with-examples-20260510/?utm_source=devto&utm_medium=post&utm_campaign=cross-post

Top comments (0)