Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

The keyword permits

The keyword permits: A permits clause lists the direct classes or interfaces allowed to extend the declared sealed type.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

permits

A permits clause lists the direct classes or interfaces allowed to extend the declared sealed type.

Use it when the permitted subtypes cannot be inferred unambiguously from the same compilation unit.

Every permitted subtype must directly inherit the sealed type and declare final, sealed, or non-sealed.

Names in permits must be available at compilation and unique; indirect descendants are not listed.

Reflection returns [Circle, Square] and sealed=true, exactly matching Shape’s permits clause.

Examples

import java.util.*; public class Main { sealed interface Shape permits Circle,Square {} record Circle(int r) implements Shape {} record Square(int s) implements Shape {} public static void main(String[] a){var names=Arrays.stream(Shape.class.getPermittedSubclasses()).map(Class::getSimpleName).sorted().toList();System.out.println(names);System.out.println("sealed="+Shape.class.isSealed());} }

Result

[Circle, Square]
sealed=true

Reflection returns [Circle, Square] and sealed=true, exactly matching Shape’s permits clause.

Sources