0Pricing
Java Academy · Lesson

Declaring Sealed Types

Restrict who can extend a type.

Declaring Sealed Types is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Sealed Types

A sealed class or interface restricts which other types may extend or implement it. This lets you model a closed set of possibilities that the compiler can reason about.

The Problem It Solves

A normal public class can be subclassed by anyone, anywhere. Sometimes you want exactly three shapes or two payment kinds, with no surprises. Sealing makes that intent explicit and enforced.

The sealed Keyword

Mark a type sealed and list its allowed subtypes with permits. Every permitted subtype must declare its own inheritance modifier.

public class Main {
    sealed interface Shape permits Circle, Square {}
    record Circle(double radius) implements Shape {}
    record Square(double side) implements Shape {}

    public static void main(String[] args) {
        Shape s = new Circle(2.0);
        System.out.println(s);
    }
}

Subtype Modifiers

Each permitted subtype must be one of: final (no further subclassing), sealed (continues the restriction), or non-sealed (reopens for free extension). Records are implicitly final.

A final Subclass

Marking a subclass final closes the hierarchy at that branch.

public class Main {
    sealed interface Animal permits Dog, Cat {}
    static final class Dog implements Animal {
        public String toString() { return "Dog"; }
    }
    static final class Cat implements Animal {
        public String toString() { return "Cat"; }
    }

    public static void main(String[] args) {
        Animal a = new Dog();
        System.out.println(a);
    }
}

A non-sealed Subclass

A non-sealed subtype deliberately reopens extension, letting unknown classes extend it again. Use it sparingly when one branch must stay open.

public class Main {
    sealed interface Plugin permits CorePlugin, ExternalPlugin {}
    static final class CorePlugin implements Plugin {}
    static non-sealed class ExternalPlugin implements Plugin {}
    static class ThirdParty extends ExternalPlugin {}

    public static void main(String[] args) {
        Plugin p = new ThirdParty();
        System.out.println(p.getClass().getSimpleName());
    }
}

Sealed Classes Too

Sealing is not limited to interfaces. An abstract sealed class works the same way and can hold shared state and behavior.

public class Main {
    sealed static abstract class Expr permits Num, Add {}
    static final class Num extends Expr {
        final int value;
        Num(int value) { this.value = value; }
    }
    static final class Add extends Expr {
        final Expr left, right;
        Add(Expr left, Expr right) { this.left = left; this.right = right; }
    }

    public static void main(String[] args) {
        Expr e = new Add(new Num(1), new Num(2));
        System.out.println(e.getClass().getSimpleName());
    }
}

Same Module or Package

Permitted subtypes must be in the same module as the sealed type, or in the same package if the code is not modular. This keeps the closed set together and verifiable.

Compiler Enforcement

If a class tries to extend a sealed type without being listed in permits, the code does not compile. The closed set is guaranteed at compile time.

Why It Matters

Because the compiler knows every subtype, it can verify that a switch over the sealed type handles all cases. This is the foundation for exhaustive pattern matching, covered in a later course.

A Complete Hierarchy

Here is a small closed hierarchy used as a value model.

public class Main {
    sealed interface Json permits JsonNull, JsonNumber, JsonString {}
    record JsonNull() implements Json {}
    record JsonNumber(double value) implements Json {}
    record JsonString(String value) implements Json {}

    public static void main(String[] args) {
        Json[] values = { new JsonNull(), new JsonNumber(3.5), new JsonString("hi") };
        for (Json j : values) {
            System.out.println(j);
        }
    }
}

Quick Check

Test your understanding of sealed declarations.

Recap

You learned to declare sealed types.

  • sealed restricts who may extend or implement a type.
  • The permits clause lists allowed subtypes.
  • Each subtype must be final, sealed, or non-sealed.
  • Subtypes live in the same module or package and are enforced by the compiler.

Frequently asked questions

Is the “Declaring Sealed Types” lesson free?

Yes — the full text of “Declaring Sealed Types” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Declaring Sealed Types”?

Restrict who can extend a type. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Declaring Sealed Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Declaring Sealed Types
  2. permits Clause
  3. Sealed with Records
  4. Exhaustive switch on Sealed
← Back to Java Academy