You can follow this link for examples.

In short, sealed classes gives you the control of which models, classes etc. that can implement or extend that class/interface.

Example from the link:

public sealed interface Service permits Car, Truck {

    int getMaxServiceIntervalInMonths();

    default int getMaxDistanceBetweenServicesInKilometers() {
        return 100000;
    }
}

This interface only permits Car and Truck to implement it.

Answer from JDTheOne on Stack Overflow
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › language › sealed-classes-and-interfaces.html
Sealed Classes
October 20, 2025 - They must have exactly one of the ... this · For example, the permitted subclasses of Shape demonstrate each of these three modifiers: Circle is final while Rectangle is sealed and Square is non-sealed....
Top answer
1 of 10
67

You can follow this link for examples.

In short, sealed classes gives you the control of which models, classes etc. that can implement or extend that class/interface.

Example from the link:

public sealed interface Service permits Car, Truck {

    int getMaxServiceIntervalInMonths();

    default int getMaxDistanceBetweenServicesInKilometers() {
        return 100000;
    }
}

This interface only permits Car and Truck to implement it.

2 of 10
38

The JEP 409 explains it as

A sealed class or interface can be extended or implemented only by those classes and interfaces permitted to do so.

A more practical explanation is the following:

The situation in the past was:

  • You could not restrict an interface being extended by another interface
  • You could not constraint which classes where able to implement a specific interface.
  • You had to declare a class as final in order to not be extended by another class. This way no class could extend the declared final class. This was black or white approach.

The current situation with sealed keyword is:

  • You can now restrict an interface being extended by other interfaces and make a rule for only some specific interfaces which will be allowed to extend it.

    Example:

    public sealed interface MotherInterface permits ChildInterfacePermitted {}
    
    //Has to be declared either as sealed or non-sealed
    public non-sealed interface ChildInterfacePermitted extends MotherInterface {}  
    
    public interface AnotherChildInterface extends MotherInterface {} 
    //compiler error! It is not included in the permits of mother inteface
    
  • You can now create an interface and select only specific classes that are allowed to implement that interface. All other classes are not allowed to implement it.

    Example:

     public sealed interface MotherInterface permits ImplementationClass1 {} 
    
     //Has to be declared either as final or as sealed or as non-sealed
     public final class ImplementationClass1 implements MotherInterface {} 
    
     public class ImplementationClass2 implements MotherInterface {} 
     //compiler error! It is not included in the permits of mother inteface
    
  • You can now restrict a class being extended (same as before with final) but you can now allow some specific classes to extend it. So now you have more control as before the keyword final was absolute restricting every class from extending the declared final class

    Example:

    public sealed class MotherClass permits ChildClass1 {}
    
    //Has to be declared either as final or as sealed or as non-sealed
    public non-sealed class ChildClass1 extends MotherClass {} 
    
     public class ChildClass2 extends MotherClass {} 
     //compiler error! It is not included in the permits of MotherClass
    

Important notes:

  • The sealed class and its permitted subclasses must belong to the same module, and, if declared in an unnamed module, to the same package.

    Example:

    Let's say that we have the same unnamed module and the following packages

      -packageA
         -Implementationclass1.java
      -packageB
         -MotherClass.java
    

    or

       -root
          -MotherClass.java
          -packageA
             -Implementationclass1.java
    

    You will get the error Class is not allowed to extend sealed class from another package. So if you have an unnamed module all participating classes and interfaces for the sealed function must be placed exactly on the same package.

  • Every permitted subclass must directly extend the sealed class.

🌐
Baeldung
baeldung.com › home › java › core java › sealed classes and interfaces in java
Sealed Classes and Interfaces in Java | Baeldung
December 11, 2025 - When creating the Vehicle abstract class in Java, we should be able to allow only Car and Truck classes to extend it. As such, we want to ensure that there will be no misuse of the Vehicle abstract class within our domain. In this example, we’re more interested in the clarity of code handling known subclasses, than defending against all unknown subclasses. Before version 15 (in which sealed classes were introduced as a preview), Java assumed that code reuse is always the goal.
🌐
Medium
medium.com › @kumar_sachin › understanding-sealed-classes-in-java-17-b542550b7d5e
Understanding Sealed Classes in Java 17 | by Sachin Kumar | Medium
February 16, 2024 - For example, in a geometry application, you might have a Shape class as sealed, and Circle, Square, and Triangle classes as its subclasses. This ensures that only these specific shapes can be created, preventing the introduction of unexpected ...
🌐
Medium
medium.com › codex › sealed-classes-in-java-17-8f00351d27f4
Sealed Classes in Java 17. An Introduction | by Afroz Chakure | CodeX | Medium
August 12, 2024 - Above, we have defined a sealed class, Shape, which allows only Circle, Square, and Triangle to extend it. public sealed interface Vehicle permits Car, Bike { void start(); } In this example, we have defined a sealed interface calledVehiclewhich ...
🌐
Medium
medium.com › @fullstacktips › sealed-classes-in-java-17-a22e4b0569e4
Sealed Classes in Java 17
February 26, 2023 - In other words, they provide more control over how a class can be extended or implemented. To understand this better, let’s take an example of a Shape class, which can be extended by Circle, Square, or Triangle classes.
🌐
DEV Community
dev.to › diogomoreira › sealed-and-final-in-java-17-4221
Sealed and Final in Java (17+) - DEV Community
February 18, 2024 - So, we add classes that can extend a sealed class with the help of permits keyword and then we list the permitted classes. In the example below, class A is marked as sealed and allows classes B and C to extend it.
Find elsewhere
🌐
Medium
medium.com › @ujjawalr › sealed-classes-in-java-17-5bb0329ce527
Sealed classes in java 17. In this post, we will explore sealed… | by Ujjawal Rohra | Medium
July 9, 2022 - Finally, it is included as a feature in java 17 (JEP 409) . ... A sealed class allows you to restrict or choose its sub-classes. A class can not extend a sealed class, if it is not in the list of permitted child classes of parent class. A class is sealed with the use of sealed keyword. A sealed class must be followed by permits keyword along with the list of classes that can extend it. Example,
🌐
Medium
medium.com › @mominsufyan7 › sealed-classes-in-java-17-8a580d5fa5ef
Sealed Classes in JAVA 17. Learn how Sealed Classes in Java 17… | by Mominsufiyan | Medium
October 16, 2025 - // Extended subclass from non-sealed class class EquilateralTriangle extends Triangle {} ... You explicitly define which classes can be part of your hierarchy. ... You prevent unwanted inheritance — especially useful in public APIs or frameworks. ... When combined with Java’s pattern matching and switch expressions, sealed classes enable exhaustive type checking at compile time.
🌐
javathinking
javathinking.com › blog › what-are-sealed-classes-in-java-17
Java 17 Sealed Classes: What They Are, How to Declare Them & Why They Matter (Previously a Preview in JDK 15) — javathinking.com
This prevents breaking changes (e.g., if a user’s subclass relies on internal behavior of the parent class that changes). For example, a geometry library might seal Shape to only permit Circle, Square, and Triangle, ensuring all shapes implement ...
🌐
Codez Up
codezup.com › home › master java 17 sealed classes: comprehensive guide
How to Implement Sealed Classes in Java 17
March 18, 2025 - They define which classes can extend or implement them using the permits keyword. This restriction helps maintain a closed hierarchy, promoting encapsulation and reducing inadvertent subclassing. When a Sealed Class is compiled, the compiler enforces that only the permitted subclasses exist within the class hierarchy.
🌐
DEV Community
dev.to › birajdar › java17-sealed-classes-4pl4
Java17 sealed classes - DEV Community
October 31, 2023 - Sealed classes are a feature introduced in Java 16 (as a preview feature) and finalized in Java 17. In this complete blog tutorial, we'll see what sealed classes are and why they're useful.
🌐
Neil Madden
neilmadden.blog › 2026 › 04 › 24 › java-sealed-classes-and-exhaustive-pattern-matching
Java sealed classes and exhaustive pattern matching – Neil Madden
April 24, 2026 - For example, here’s a toy example using a sealed interface and records (inner classes are implicitly added to the permitted sub-types if an explicit list is not given): public sealed interface SealedType { record TypeA()…
🌐
TechWell
techwell.com › techwell-insights › 2021 › 09 › sealed-classes-java-17
Sealed Classes in Java 17 | TechWell
September 15, 2021 - Sealed classes in Java 17 is a new feature and not a “preview” feature. A class is declared sealed using the modifier sealed. The classes that are allowed to extend a sealed class are declared with the modifier permits. The sealed and permits are not keywords but identifiers.
🌐
LinkedIn
linkedin.com › pulse › sealed-classes-java-17-afroz-chakure-xvxqf
Sealed Classes in Java 17
February 7, 2024 - In above example, we have defined a class Circlewhich extends sealed class Shape.The Circle class has a parameterized constructor that takes radius as parameter and initializes the radius field.
🌐
HappyCoders.eu
happycoders.eu › java › sealed-classes
Sealed Classes in Java
June 12, 2025 - So the following is valid Java code: public void sealed() { int permits = 5; }Code language: Java (java) In Java 17, "Pattern Matching for switch" was introduced as a preview feature.
🌐
C# Corner
c-sharpcorner.com › article › sealed-classes-a-java-17-new-feature
Sealed Classes - A Java 17 New Feature
September 5, 2022 - So here comes the concept called sealed classes where we can mention which are subclasses or sub interfaces which can inherit from this particular class and interface and we can do this with the help of sealed keyword.