Sealed Classes in Java 17 | TechWell

Sealed Classes in Java 17

Coffee and Code

Sealed classes are classes that permit only specific classes to extend them, as a result limiting extensibility. Sealed classes provide several benefits such as:

  • Provide additional modularization

  • Could be used to develop internal classes

  • Could be used to develop proprietary software


Problem
Java provides two access levels: public and the default (package-private) if no modifier is specified. If a class has the public access modifier, it is accessible anywhere the class package is accessible and extensible by any class. If a class has the package-private access level (default if no access modifier), it is accessible and extensible only within the package the class is declared in. Java does not provide any fine-grained access control.

Solution
Java 17 introduces sealed classes for fine-grained extensibility. Sealed classes in Java 17 is a new feature and not a “preview” feature. A class is declared sealed using the modifier sealed. The classes that are allowed to extend a sealed class are declared with the modifier permits. The sealed and permits are not keywords but identifiers. Sealed and permits are restricted identifiers as these are usable only in limited contexts. A class that extends a sealed class must have been declared in the permits list. A class that extends a sealed class must use one of the three modifiers: sealed, non-sealed, and final. The non-sealed modifier is a new keyword—in fact the only hyphenated keyword. Sealed classes may be used with record classes. Pattern matching for switch statements may include sealed classes.

A Simple Example 
Suppose that a class called Polygon represents all polygons. Presumably, a class called Line that represents a geometric line wouldn’t benefit by extending the Polygon class. Rather than allowing just about any class to extend the Polygon class, you want that only certain specific classes may extend the class. You may want to limit the extensibility of the Polygon class to two other classes: Triangle and Quadrilateral. You would declare a sealed class Polygon as follows:
public abstract sealed class Polygon permits Triangle, Quadrilateral  { …}

Requirements for a  Sealed Class
A sealed class:

  • Must be declared with the sealed modifier

  • The sealed modifier cannot be mixed with the final or the non-sealed modifier, which means that a sealed class cannot be declared final.

  • Must have subclasses

  • The permitted classes, which are the permitted subtypes of the sealed class, cannot be anonymous or local classes—which means that the permitted classes, if any, must have a canonical name.


Requirements for an Extending Class
A class that extends a sealed class :

  • Must be listed in the permits clause of the sealed class declaration

  • Must specify one of three modifiers: final, sealed, or non-sealed


If an extending class is declared final it cannot be extended further. If an extending class is declared sealed it is a sealed class extending a sealed class. If an extending class is declared non-sealed, it breaks the seal, which means that any class may extend a non-sealed class.

Non-Requirements
Sealed classes do not impose certain restrictions, what may be called non-requirements:

  • The permitted subclasses may be declared in a separate source file or the same source file as the sealed class as nested or auxiliary classes. If the permitted classes are declared in the same source file as the sealed class, the permits clause in the sealed class declaration may be omitted. The compiler discovers the permitted classes from the same source file.

  • A permitted subtype of a sealed class does not have to extend the sealed class.

  • A permitted subtype does not have to be declared, which means that the classes declared in the permits clause of a sealed class may not exist at all.

  • A sealed class doesn't have to be a class. An interface may be declared as sealed using the sealed identifier. 


Sealed classes seal the deal for a class, so to speak, for class extensibility.

Up Next

August 30, 2021
7 comments

kriti shah's picture
September 23, 2021 - 6:34am

Pankaj Nagla's picture
September 24, 2021 - 1:56am

Apoorva Gupta's picture
September 28, 2021 - 8:00am

Nikhil sharma's picture
September 30, 2021 - 4:13am

vishesh paliwal's picture
September 30, 2021 - 8:42am

vishesh paliwal's picture
new October 2, 2021 - 10:13am

Nikhil sharma's picture
new October 3, 2021 - 3:19am

About the Author

TechWell Insights To Go

(* Required fields)

Get the latest stories delivered to your inbox every week.