0Pricing
Java Academy · Lesson

permits Clause

List permitted subtypes.

permits Clause is a free Java Academy lesson on CoddyKit — lesson 2 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.

The permits Clause

The permits clause is how a sealed type names its allowed subtypes. It comes right after the type name and the permits keyword.

Basic Syntax

List the permitted subtypes separated by commas after permits.

public class Main {
    sealed interface Vehicle permits Car, Truck, Motorcycle {}
    record Car() implements Vehicle {}
    record Truck() implements Vehicle {}
    record Motorcycle() implements Vehicle {}

    public static void main(String[] args) {
        Vehicle v = new Truck();
        System.out.println(v.getClass().getSimpleName());
    }
}

Implementing the Contract

Each named subtype must actually implements (for interfaces) or extends (for classes) the sealed type. Naming a class in permits that does not is a compile error.

Omitting permits

If all permitted subtypes are declared in the same file as the sealed type, you may omit permits entirely. The compiler infers the list automatically.

public class Main {
    sealed interface Status {}
    record Active() implements Status {}
    record Inactive() implements Status {}

    public static void main(String[] args) {
        Status s = new Active();
        System.out.println(s.getClass().getSimpleName());
    }
}

When permits Is Required

If subtypes live in separate files within the same package or module, you must list them explicitly in permits. The compiler cannot infer across files.

Order Does Not Matter

The order of names in permits has no semantic meaning. It does not affect switch behavior or anything else; it is purely a list of allowed types.

A Class Hierarchy with permits

The same clause works for sealed classes with abstract behavior.

public class Main {
    sealed static abstract class Node permits Leaf, Branch {
        abstract int size();
    }
    static final class Leaf extends Node {
        int size() { return 1; }
    }
    static final class Branch extends Node {
        final Node a, b;
        Branch(Node a, Node b) { this.a = a; this.b = b; }
        int size() { return a.size() + b.size(); }
    }

    public static void main(String[] args) {
        Node tree = new Branch(new Leaf(), new Branch(new Leaf(), new Leaf()));
        System.out.println("Leaves: " + tree.size());
    }
}

Forgetting a Subtype

If a class implements the sealed interface but is missing from permits, compilation fails with a message saying the class is not allowed to extend the sealed type.

Permitted Types Are Public Knowledge

The permits list is part of the type's contract and is recorded in the class file. Tools and the compiler can read it to know the complete set of subtypes.

Mixing final and sealed Children

A permits list can mix modifiers: some children final, others sealed to continue the hierarchy deeper.

public class Main {
    sealed interface Shape permits Circle, Polygon {}
    record Circle(double r) implements Shape {}
    sealed interface Polygon extends Shape permits Triangle, Rectangle {}
    record Triangle() implements Polygon {}
    record Rectangle() implements Polygon {}

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

Best Practices

Keep the sealed type and its subtypes close together for readability. Prefer omitting permits for small single-file hierarchies, and list explicitly when files are split.

Quick Check

Test your understanding of the permits clause.

Recap

You learned the permits clause.

  • It lists the allowed subtypes after the sealed type name.
  • Each listed type must extend or implement the sealed type.
  • It can be omitted when all subtypes share the same file.
  • Subtypes can themselves be sealed to extend the hierarchy.

Frequently asked questions

Is the “permits Clause” lesson free?

Yes — the full text of “permits Clause” 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 “permits Clause”?

List permitted subtypes. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “permits Clause” 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