Sealed Classes in Java 17

An Introduction

Afroz Chakure
3 min readJan 29, 2024
Photo by Michiel Leunens on Unsplash

What are Sealed Classes?

Sealed classes are a feature introduced in Java 16 (as a preview feature) and finalized in Java 17.

Sealed classes allow developers to restrict the inheritance hierarchy of a class or interface. In other words, they provide more control over how a class can be extended or implemented.

Code Example for a Sealed class

Take a Shapeclass, which can be extended by Circle, Square, or Triangle classes. With Sealed Classes, we can specify that only these three classes can inherit from Shape, and no other class can extend it.

Check syntax below:

public sealed class Shape permits Circle, Square, Triangle {
// Class definition
}

Above, we have defined a sealed class, Shape, which allows only Circle, Square, and Triangle to extend it.

1. Defining a sealed class

public sealed interface Vehicle permits Car, Bike {
void start();
}

In this example, we have defined a sealed interface calledVehiclewhich permits only Car and Bike classes to implement it. The interface defines a single method called start().

2. Extending a sealed class

public final class Circle extends Shape {
private final double radius;

public Circle(double radius) {
this.radius = radius;
}

// Class definition
}

In above example, we have defined a class Circlewhich extends sealed class Shape.The Circle class has a parameterized constructor that takes radius as parameter and initializes the radius field.

3. Attempting to Extend an Unpermitted Class

public class Rectangle extends Shape {
private final double width;
private final double height;

public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

// Class definition
}

In this example we have defined a class Rectangle, which extends sealed class Shape.However, since Rectangle is not listed as one of the permitted classes, we get a compile-time error.

Features of Sealed Classes:

  1. It helps creating a finite set of classes in inheritance.
  2. Sealed classes specify which classes can extend them, providing a clear hierarchy.
  3. They ensure that only permitted sub-classes are created, avoiding unexpected behavior.
  4. Useful for creating frameworks, allows developers to dictate which classes users can extend.
  5. Especially beneficial in large projects, it prevents unintentional changes to important classes.

Benefits of Sealed Classes:

Sealed Classes provide several benefits, including:

  1. Enhanced Security: With Sealed Classes, developers can restrict the inheritance hierarchy of a class or interface, preventing unauthorized access to sensitive code.
  2. Better Maintainability: By restricting the inheritance hierarchy, Sealed Classes promote code maintainability by making it easier to understand and modify the code-base.
  3. Improved Performance: Sealed Classes enable the JVM to make more optimizations at runtime, leading to improved performance.

Important points on Sealed classes:

  1. Sealed class must specify it’s permitted sub-class using permits clause.
  2. Sealed class must have sub-classes.
  3. Only the listed sub-classes are allowed to extend the sealed class.
  4. Permitted sub-classes must be in same package.
  5. Permitted sub-classes should extends their sealed super class directly.
  6. Permitted sub-classes should extends their sealed super class directly.

--

--