Core Java

Java 15: Sealed Classes

Java 15 introduces Sealed Classes, a preview language feature, that allows classes/interfaces to restrict which other classes/interfaces may extend or implement them. Here is an example:

1
2
3
4
5
public sealed class Vehicle permits Car, Truck, Motorcycle { ... }
 
final class Car extends Vehicle { ... }
final class Truck extends Vehicle { ... }
final class Motorcycle extends Vehicle { ... }

In the example above, Vehicle is a sealed class, which specifies three permitted subclasses; Car, Truck and Motorcycle.

The subclasses must be:

  • in the same package or module as the superclass. You can even define them in the same source file as the superclass (if they are small in size), in which case the permits clause is not required because the compiler will infer them from the declarations in the file.
  • declared either final (i.e. cannot be extended further), sealed (i.e. permit further subclasses in a restricted fashion) or non-sealed (i.e. open for extension by any class).

Sealing serves two main purposes:

  1. It restricts which classes or interfaces can be a subtype of a class or interface and thus preserves the integrity of your API.
  2. It allows the compiler to list all the permitted subtypes of a sealed type (exhaustiveness analysis), which will (in a future Java release) enable switching over type patterns in a sealed type (and other features). For example, given the following switch statement, the compiler will detect that there is a case statement for every permitted subclass of Vehicle (so no default clause is needed) and it will also give an error if any of them are missing:
    1
    2
    3
    4
    5
    6
    7
    int doSomething(Vehicle v) {
      return switch (v) {
          case Car c -> ...
          case Truck t -> ...
          case Motorcycle m -> ...
      };
    }

Published on Java Code Geeks with permission by Fahd Shariff, partner at our JCG program. See the original article here: Java 15: Sealed Classes

Opinions expressed by Java Code Geeks contributors are their own.

Fahd Shariff

Fahd is a software engineer working in the financial services industry. He is passionate about technology and specializes in Java application development in distributed environments.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button