0Pricing
Java Academy · Lesson

permits Clause

List permitted subtypes.

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());
    }
}

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