Skip to main content

Lesson 7 of 9

Sealed Classes (Java 17+)

Sealed classes restrict which classes can extend them. Combined with pattern matching and switch expressions, they enable exhaustive type hierarchies where the compiler verifies you've handled all cases - like a type-safe enum with full class features.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Sealed Classes in Practice

Sealed Classes in Practice
// Sealed class - only BookType subclasses are allowed
public sealed class LibraryItemType
    permits PhysicalBook, EBook, AudioBook, Magazine {

    private final String itemId;
    private final String title;

    public LibraryItemType(String itemId, String title) {
        this.itemId = itemId;
        this.title  = title;
    }

    public String getItemId() { return itemId; }
    public String getTitle()  { return title; }
}

// Permitted subclasses - each must be final, sealed, or non-sealed
public final class PhysicalBook extends LibraryItemType {
    private final String isbn;
    public PhysicalBook(String isbn, String title) {
        super(isbn, title);
        this.isbn = isbn;
    }
    public String getIsbn() { return isbn; }
}

public final class EBook extends LibraryItemType {
    private final String downloadUrl;
    public EBook(String id, String title, String url) {
        super(id, title);
        this.downloadUrl = url;
    }
    public String getDownloadUrl() { return downloadUrl; }
}

public final class AudioBook extends LibraryItemType {
    private final int durationMinutes;
    public AudioBook(String id, String title, int dur) {
        super(id, title);
        this.durationMinutes = dur;
    }
    public int getDurationMinutes() { return durationMinutes; }
}

public final class Magazine extends LibraryItemType {
    private final int issueNumber;
    public Magazine(String id, String title, int issue) {
        super(id, title);
        this.issueNumber = issue;
    }
}

// Exhaustive switch - compiler verifies ALL permitted subclasses handled
public static String getLabel(LibraryItemType item) {
    return switch (item) {  // No default needed - compiler catches unhandled cases
        case PhysicalBook b -> "Book: ISBN=" + b.getIsbn();
        case EBook e        -> "EBook: URL=" + e.getDownloadUrl();
        case AudioBook a    -> "Audio: " + a.getDurationMinutes() + " min";
        case Magazine m     -> "Magazine: " + m.getTitle();
    };
}

Quick Quiz

Tip

Tip

Practice Sealed Classes Java 17 in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Sealed Classes (Java 17+)

Practice Task

Note

Practice Task - (1) Write a working example of Sealed Classes Java 17 from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Sealed Classes Java 17 is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Topics in This Module

0/9 done