Home » Java » Core Java » Java 15: Sealed Classes

About Fahd Shariff

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.

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.

(+1 rating, 1 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
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